I'm trying to use a Cocoa Framework (MultitouchSupport, to be specific) from within a basic Objective-C++ file, but I keep getting undefined symbol errors, as if g++ is supposed to have different linker flags than gcc.
My ultimate goal is to integrate a C++ networking library with some basic Objective-C code I got from here: http://steike.com/code/multitouch/.
When I run this to compile the original code, it works fine:
gcc -F/System/Library/PrivateFrameworks -framework MultitouchSupport test.m -o test -std=c99
But when I rename the file to test.mm, so that it can later include and reference C++ files, the following doesn't work:
g++ -F/System/Library/PrivateFrameworks -framework MultitouchSupport test.mm -o test
And gives me these errors:
Undefined symbols for architecture x86_64:
"MTDeviceCreateDefault()", referenced from:
_main in ccq0vzuM.o
"MTRegisterContactFrameCallback(void*, int (*)(int, Finger*, int, double, int))", referenced from:
_main in ccq0vzuM.o
"MTDeviceStart(void*, int)", referenced from:
_main in ccq0vzuM.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [testpp] Error 1
What do I need to do in order for this Objective-C++ file to see the Framework I'm looking for so I can use C++ with it?
Thanks!
In the header that declares those three functions, is there an extern "C" block that wraps them? Something like this?
#ifdef __cplusplus
extern "C" {
#endif
// function declarations here
#ifdef __cplusplus
}
#endif
If not: You could add one to the header file, or add a similar wrapper around your #import of that header file.
Related
At first I am trying to use glad.h in my project. Xcode 7.2.1. gives me the error :
glad.h:26:2: OpenGL header already included, remove this include, glad already provides it.
These are the headers in my main.cpp :
#include "glad/glad.h"
#include <iostream>
#include "GLFW/glfw3.h"
I add GLFW_INCLUDE_NONE to packaging - preprocessor definitions and LLVM - preprocessor macros(similar to preprocessor definitions in Visual Studio I guess?). Then the errors turn to:
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.o
"_glfwGetPrimaryMonitor", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I remove the code #include "glad/glad.h”, the errors remain the same.
Any help is appreciated.
I am very new to C++ to say the least. However, I cannot find answer to this specific question anywhere.
This is not code specific either, as I tried downloading a lot of code and compiling it via g++ or just make.
It seems that whenever i include a class header in my main() it throws the linker error:
Undefined symbols for architecture x86_64:
"House::getNumBath()", referenced from:
printHouse(House) in houses-91268b.o
"House::getSqft()", referenced from:
printHouse(House) in houses-91268b.o
"House::getColor()", referenced from:
printHouse(House) in houses-91268b.o
"House::House(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, int, double)", referenced from:
_main in houses-91268b.o
"House::House()", referenced from:
_main in houses-91268b.o
"House::~House()", referenced from:
_main in houses-91268b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It happens no matter what code do I download or produce and seems to be related to the constructor function implemented as MyClass::MyClass(/* args */);
Someone suggested that I should include .cpp file in my main instead of .h file, and yes it works. But it's very wrong as I've always been taught that interfaces are meant for it. Otherwise what's their purpose?
With another person suggestion (googled), I am now downloading 10GB worth of Xcode, and hope it helps. Still seems like it's not going to work. What is wrong with C++ here? Am I for some reason missing C++ 11 (again, why on the relatively new OS?) ?
UPDATE:
I grabbed the code from another tutorial that throws me the same thing:
dog.cc
#include "Dog.h"
Dog::Dog() {
}
dog.h
#ifndef DOG_H
#define DOG_H
class Dog {
public:
Dog();
protected:
private:
};
#endif
main.cc
#include "Dog.h"
int main() {
Dog barky;
return 0;
}
Throws both the same after me doing (they are in the same folder, those):
g++ main.cc
or
make ./main
It is probably the simplest example I wanted to try, as I never had problems like this before in C, either using Makefile or by just cc the file.
You need to compile the object (dog.o) file first:
g++ -c dog.cc
Then compile the main with it:
g++ main.cc dog.o
I am trying to use LZF C++. However, when I compile it, it give me this error:
g++ -o dist/Debug/GNU-MacOSX/cppapplication_2 build/Debug/GNU-MacOSX/lzf_c.o build/Debug/GNU-MacOSX/lzf_d.o build/Debug/GNU-MacOSX/main.o
Undefined symbols for architecture x86_64:
"lzf_compress(void const*, unsigned int, void*, unsigned int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
I already included lzf.h.
A quick look at lzf.h shows that it doesn't account for C++. Normally you would see something like
#ifdef __cplusplus
extern "C" {
#endif
/* Function declarations go here */
#ifdef __cplusplus
}
#endif
So that the C++ compiler knows to look for C symbols. I'm guessing what is happening with your code is that the C++ compiler is requesting whatever it mangles the C++ symbol lzf_compress to instead of just the C symbol lzf_compress. However, since the actual code is in a C file your build system probably uses the C compiler (not the C++ compiler) to compile it.
If I were you I would fix the header, and file a bug (with a patch) to get the fix upstream.
If you don't want to fix lzf.h I think you could just do something like
extern "C" {
#include "lzf.h"
}
The other "solution" would be to just compile everything with the C++ compiler. I'm not certain liblzf is valid C++, though, so that may or may not work.
I'm starting with OpenGL in c++, and I included GLUT and OpenGL frameworks on my project on XCode. When I try to compile and run my program as if it was a simple c++ program it gives me this error:
Undefined symbols for architecture x86_64:
"_glBegin", referenced from:
display() in main.o
"_glClear", referenced from:
display() in main.o
"_glClearColor", referenced from:
etc.
Using a simple c++ program, I don't get any of this errors. I searched the web and I found that I need to compile with g++ (specific for c++ lenguage), but I don't know how to do it. Any help?
You need to add -framework OpenGL to your compiler settings.
I am using Lear's implementation of the Gist descriptor for a project which can be found here: http://lear.inrialpes.fr/software.
I am writing an application in c++ and I want to use this library. I am having issues though with the makefile and linking in general.
These commands give me no errors:
g++ -c standalone_image.c -o standalone_image.o
g++ -c gist.c -o gist.o
However, this line
g++ compute_gist.c `pkg-config --cflags --libs opencv`
gives me the following error
Undefined symbols for architecture x86_64:
"color_gist_scaletab(color_image_t*, int, int, int const*)", referenced from:
_main in ccMFYbAU.o
"color_image_delete(color_image_t*)", referenced from:
_main in ccMFYbAU.o
"color_image_new(int, int)", referenced from:
load_ppm(char const*)in ccMFYbAU.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I have the Mosaic c++ code in a different directory. I also tried to compile gist and standalone_image seperately, copy into the mosaic directory, and compile the Mosaic code.
Which gives me the following error:
Undefined symbols for architecture x86_64:
"color_gist_scaletab(color_image_t*, int, int, int const*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [mosaic] Error 1
I really want to use this library in my project, but I can't figure out a way to incorporate it in my c++.
Any help is GREATLY appreciated! Thanks!
Edit: I am using Mac Lion with:
gcc + g++ version: i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
The lear library also uses the FFTW3 library that should work with C and C++.
The problem was that I needed the extern around the gist include, but the linking that was done in the Makefile was also wrong. It works now. :)