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.
Related
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
Undefined symbols for architecture x86_64:
"makeHero(std::string, int)", referenced from:
makeCard() 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)
This is the error I am getting when I'm trying to compile my code on Xcode. I've looked around and I have changed Architecture settings to Universal, and have made c++ std library, libstdc++, which were answers I found on here. Still nothing :/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
Is the beginning of my main.cpp file whats causing it? I changed stdlib.h and stdio.h to without the .h and I get stdlib file not found.
Undefined symbols for architecture x86_64
This error message is rather misleading, what it's really telling you is that the compiler has seen a function declaration (symbol) for makeHero(std::string, int), but can't find its implementation.
The error is simple to reproduce by declaring a function in a header file, without implementing the function's body in either the header or cpp.
To fix the problem, ensure the body for the function is implemented in the project.
I have just started working with Luabind, and have attempted to run the Hello World test specified at http://www.rasterbar.com/products/luabind/docs.html#calling-lua-functions. However, this provides an undefined symbol error when attempted to compile.
Undefined symbols for architecture x86_64:
"luabind::scope::scope(std::__1::auto_ptr<luabind::detail::registration>)", referenced from:
luabind::scope luabind::def<void (*)(), luabind::detail::null_type>(char const*, void (*)(), luabind::detail::null_type const&) in TestClass.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Other functions such as luabind::open and luabind::call_function work correctly.
I installed both luabind and lua 5.1 through homebrew on osx.
It looks to me like you are compiling your program against libc++ and trying to link to a luabind library that was compiled against the stdlibc++ library.
The clue is std::__1::auto_ptr. This __1 inline namespace is used by libc++ to differentiate it's ABI from stdlibc++
Thus if
luabind::scope::scope(std::__1::auto_ptr<luabind::detail::registration>)
Cannot be found it is likely because libluabind doesn't have it. Dumping it's exported symbols you might find it has
luabind::scope::scope(std::auto_ptr<luabind::detail::registration>)
Instead.
If I am right, simply recompile libluabind targeting libc++ and you should find it works with your test program.
I'm trying to compile some C++ code on my mac. I can run it using Xcode, but when I try to compile from the terminal using gcc I get the following error:
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccOJDOlb.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccOJDOlb.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccOJDOlb.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccOJDOlb.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Anyone know what might be going on?
ld: symbol(s) not found for architecture x86_64
This is saying that 'ld', the linker, is not able to find definitions for some symbols your program uses. So you know compilation has succeeded.
And ld lists the specific symbols. Notice that they are all from the standard library. This should tell you that the standard library is probably not being found by the linker.
Typically when you use a library you have to tell the compiler toolchain using a link flag. But for a language's standard library the compilers in the GNU compiler collection generally just assume you want the library for that language. So running the gcc command automatically includes the C standard libraries, running g++ automatically includes the C++ standard libraries, etc. But notice, running 'gcc' does not automatically link in the C++ standard library. It's very likely that you're running the command 'gcc' and simply not adding the correct linker flag for the C++ standard library.
If for some reason you want to use gcc and not g++ you'll have to explicitly state that you want the standard library, using a flag like -lstdc++.
Also, unless you really want gcc and you're installing the latest versions of it yourself on OS X you may want to switch over to clang/clang++ and the libc++ implementation of the C++ standard library. The gcc that comes with Xcode is based on an old version, and the standard library is similarly old. Xcode has been transitioning to clang as the system compiler for some time now. The clang compiler driver has basic compatibility with gcc so you use many of the same basic flags. Here's a good set to start with:
clang++ -std=c++11 -stdlib=libc++ -Wall -Wextra
I try to compile from the terminal using gcc
Then try g++ instead of plain ol' gcc. The former automatically links against the C++ standard library, the latter does not.
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.