Compiling with Clang using Libc++ undefined references - c++

The first couple are too long to reference. I get this error when I try to compile clang++ -stdlib=libc++ ../main.cc ... with clang and libc++ from the SVN.
error: undefined reference to 'typeinfo for char const*'
error: undefined reference to '__cxa_allocate_exception'
error: undefined reference to '__cxa_throw'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_begin_catch'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_rethrow'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_end_catch'
/tmp/cc-pbn00y.o(.eh_frame+0xbd3): error: undefined reference to '__gxx_personality_v0'
SOLUTION: Thanks to one of the answers, I know the solution. libc++ can't be used by itself like libstdc++, it has to be linked along with libc++abi. However, libc++abi isn't complete yet, so using libc++ seems to be a little incomplete for the moment, but it is still my first choice when it completes.
UPDATE 5/26/2012: libc++abi is now complete for C++ and I have been using clang++ as follows successfully clang++ -std=c++11 -stdlib=libc++ -lc++abi.

I believe libc++ doesn't support all exception functions yet. See the status page:
http://libcxxabi.llvm.org/spec.html
You could probably link against gnu's libstdc++

Here's what works for me with the Ubuntu Vivid packages for clang and libc++:
clang++ -std=c++11 -stdlib=libc++ <object files> -lc++abi -lsupc++
It is important that the object files come before the -l flags, e.g. when you use exceptions. Obviously, this still will not link if you use libraries compiled against libstdc++ and use any STL types in those interfaces.

This seems like you are using exception handling, but it isn't enabled in the compiler. Try passing -fexceptions to the commandline.

I'm only adding this answer as I literally made this mistake just now. It was compiling most of what I was writing just fine for days, but now it starts throwing undefined reference errors...
So... I... sorta possibly was compiling with clang not clang++. Yeah. That was all that was wrong. clang++ includes C++ library stuff, clang doesn't. Oops!

Related

GCC undefined references with abi:cxx11

I'm compiling C++ programs with gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11). Everything is fine at compile time.
I then link those programs with a library that was built with gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3).
I then get undefined reference link errors:
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyGRVelocityProjectionCollisionResolver.cpp.o:(.rodata._ZTV46RigidBodyGRVelocityProjectionCollisionResolver[_ZTV46RigidBodyGRVelocityProjectionCollisionResolver]+0x18): undefined reference to `RigidBodyGRVelocityProjectionCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyGRLCPCollisionResolver.cpp.o:(.rodata._ZTV31RigidBodyGRLCPCollisionResolver[_ZTV31RigidBodyGRLCPCollisionResolver]+0x18): undefined reference to `RigidBodyGRLCPCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyVelocityProjectionCollisionResolver.cpp.o:(.rodata._ZTV44RigidBodyVelocityProjectionCollisionResolver[_ZTV44RigidBodyVelocityProjectionCollisionResolver]+0x18): undefined reference to `RigidBodyVelocityProjectionCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyLCPCollisionResolver.cpp.o:(.rodata._ZTV29RigidBodyLCPCollisionResolver[_ZTV29RigidBodyLCPCollisionResolver]+0x18): undefined reference to `RigidBodyLCPCollisionResolver::getName[abi:cxx11]() const'
CMakeFiles/FOSSSim.dir/RigidBodies/RigidBodyAllPairsCollisionDetector.cpp.o:(.rodata._ZTV34RigidBodyAllPairsCollisionDetector[_ZTV34RigidBodyAllPairsCollisionDetector]+0x18): undefined reference to `RigidBodyAllPairsCollisionDetector::getName[abi:cxx11]() const'
With some googling, I found some links like linking-problems-due-to-symbols-with-abicxx11 dealing with the -D_GLIBCXX_USE_CXX11_ABI=0 compiling option. Unfortunately, when I use that option, I get a Cmake warning:
CMake Warning:
Manually-specified variables were not used by the project:
_GLIBCXX_USE_CXX11_ABI
Any clue on the way to solve the issue? Knowing that I would prefer avoiding to change compiler version.
Thanks.
_GLIBCXX_USE_CXX11_ABI is a preprocessor macro that controls the behavior of the GNU C++ standard library implementation. It needs to be passed to the preprocessor, not CMake.
Use add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) in your CMakeLists.txt or pass it via CMAKE_CXX_FLAGS on the command line:
cmake -DCMAKE_CXX_FLAGS='-D_GLIBCXX_USE_CXX11_ABI=0' path/to/your/project
Note that while this may get your program to build, it likely won't be enough to make everything work. Mixing object code compiled by different versions of GCC is fraught with issues.

Boost regex: Undefined references when linking

Until some weeks ago, using the boost_regex library I used to compile a C++ file test.cpp using the following command.
g++-4.9 -Wall -O3 -march=native -flto -DNDEBUG -o test \
--std=c++14 test.cpp -lboost_regex -pthread
The compilation was working perfectly. But at some point, I needed to upgrade my OS (more precisely, it was Ubuntu 14.04, now it is Ubuntu 16.04).
I kept my local folders with my data, and I also installed the Boost library again in the new system, including boost_regex.
The Boost version in the new system is 1.58, unfortunately I cannot know which Boost version I was using before because it is now deleted.
I try to compile again the same file in the new system, with the same command as above, and the linker says it cannot find two functions called maybe_assign and construct_init. If I replace the -o test option with just -c (i.e. without linking) then the program is compiled without errors.
More precisely, when I try to do linking I get the following errors (they were extremely long, I hade to shorten using ... dots).
In function `boost::re_detail::perl_matcher<...>::match_match()':
undefined reference to `boost::match_results<...>::maybe_assign(boost::match_results<...> const&)'
In function `bool boost::regex_search<...>(...)':
undefined reference to `boost::re_detail::perl_matcher<...>::construct_init(...)'
In function `boost::re_detail::perl_matcher<...>::match_prefix()':
undefined reference to `boost::match_results<...>::maybe_assign(boost::match_results<...> const&)'
collect2: error: ld returned 1 exit status
I really don't know how this is possible, the libraries seem perfectly installed, nothing seems missing, and the compilation was working with a previous OS installation (then I guess older libraries).
At these point my only guess could be that Boost authors removed such functions? (maybe they are obsolete?). But I didn't find any trace of this on internet. If this is the case, how can I know the boost versions in which such functions are available?
Am I doing any mistake? Do you have any suggestions to investigate about this?
TL;DR use gcc5.
Ununtu 16.04 comes with gcc5 by default. Every C++ library in it is compiled with that compiler.
Now there was an incompatible C++ ABI change between gcc4 and gcc5. It made binaries built from C++ sources with gcc4 incompatible by default with binaries built with gcc5. This incompatibility often manifests itself as a bunch of undefined symbols that reference std::string and/or std::list.
The standard library comes built with a dual ABI to support objects built with older compilers. Other libraries like boost, hovever, don't.

GCC 4.8 fails while linking with libmagic

I am struggling with the issue while linking my code against libmagic:
test.c:(.text+0x16): undefined reference to `magic_open'
test.c:(.text+0x50): undefined reference to `magic_load'
test.c:(.text+0x60): undefined reference to `magic_error'
test.c:(.text+0x84): undefined reference to `magic_close'
test.c:(.text+0x9e): undefined reference to `magic_file'
test.c:(.text+0xba): undefined reference to `magic_close'
collect2: ld returned 1 exit status
However the issue appears only when gcc version > 4.4. To compile, I am using the the following command:
gcc -L/usr/lib/ -lmagic test.c -o test
An example code that uses libmagic might be found here. I have checked and this issue appears as well. Obviously the libmagic and libmagic-dev are installed on my system (Ubuntu 14.04).
Is there any way of handling this issue different thant downgrading gcc version?
This is a FAQ, unrelated to your version of GCC.
I don't think that your compilation succeeded with gcc-4.3
Order of arguments to gcc matter a lot (see e.g. this); object files and libraries should go last (from the high-level to the low-level ones). Try with
gcc -Wall -g test.c -lmagic -o mytest
BTW, don't call your executable test (but e.g. mytest) since test is often a shell builtin.

zkcm-library not finding the mpfr-library

I just installed the zkcm library on my kubuntu machine and I'm having trouble compiling c++ code.
I have installed the gmp and mpfr libraries and checked that they work; the code
mpfr_t m1, m2, m3;
mpfr_init(m1); mpfr_init(m2); mpfr_init(m3);
mpfr_mul(m1, m2, m3, MPFR_RNDN);
compiles and runs.
I then try to use zkcm; I try compliling the line
zkcm_matrix m;
and get a bunch of errors seemingly about zkcm not finding mpfr; here is the beginning of the output:
/usr/local/lib/libzkcm.a(zkcm_c.o): In function `zkcm_init_ri(zkcm*, double, double)':
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:126: undefined reference to `mpfr_inits'
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:127: undefined reference to `mpfr_set_d'
/usr/local/lib/libzkcm.a(zkcm_c.o): In function `zkcm_init_ri_str(zkcm*, char const*, char const*)':
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:132: undefined reference to `mpfr_inits'
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:136: undefined reference to `mpfr_set_str'
/home/jorgen/Downloads/zkcm_lib-0.4.0/srcs/zkcm_c.c:134: undefined reference to `mpfr_set_ui'
I have tried to follow the instructions infrom zkcm; I have the lines
#include "mpfr.h"
#include "zkcm.hpp"
and I compile the code (called test.cpp) using
g++ test.cpp -msse2 -std=c++11 -O2 -lm -lmpfr -lgmp -lgmpxx -lzkcm -o test
Any ideas?
The library order is incorrect: according to the error message, zkcm uses MPFR, so that -lzkcm should be put before -lmpfr (which itself should be put before -lgmp because MPFR uses GMP).
Otherwise the following happens: If the linker founds a MPFR symbol that is not used by test.cpp (or some dependency), it will drop it. And if such a symbol is used by zkcm, this will yield an error since -lzkcm comes later in the command line. This also explains why you may get errors for some MPFR symbols and not others (and errors may appear and disappear when the test.cpp code and the zkcm code change).
This should be sufficient to solve the problem here. But look at this answer to "Linker order - GCC" for more general rules (this answer also deals with cyclic dependencies).

Getting huge error spew from GtkD on a simple program

I installed GtkD from AUR, and now I'm trying to compile this tutorial code. Both for compiling GtkD itself and the tutorial code, I'm using GDC 4.9.1.
I attempted to compile the code as follows (hello.d is the file name):
gdc -Wall -Werror -Wdeprecated -Wunknown-pragmas -g -m64 `pkg-config --cflags --libs gtkd-2` -O3 -frelease -o hello
But when I try that, I get this:
/tmp/ccxejYOB.o: In function `_Dmain':
/home/koz/Documents/Programming/D/gtkd/hello.d:23: undefined reference to `_D3gtk4Main4Main4initFKAAyaZv'
/home/koz/Documents/Programming/D/gtkd/hello.d:24: undefined reference to `_D3gtk10MainWindow10MainWindow7__ClassZ'
/home/koz/Documents/Programming/D/gtkd/hello.d:24: undefined reference to `_D3gtk10MainWindow10MainWindow6__ctorMFAyaZC3gtk10MainWindow10MainWindow'
/home/koz/Documents/Programming/D/gtkd/hello.d:26: undefined reference to `_D3gtk5Label5Label7__ClassZ'
/home/koz/Documents/Programming/D/gtkd/hello.d:26: undefined reference to `_D3gtk5Label5Label6__ctorMFAyabZC3gtk5Label5Label'
/home/koz/Documents/Programming/D/gtkd/hello.d:28: undefined reference to `_D3gtk4Main4Main3runFZv'
/tmp/ccxejYOB.o:(.data+0x30): undefined reference to `_D3gtk10MainWindow12__ModuleInfoZ'
/tmp/ccxejYOB.o:(.data+0x38): undefined reference to `_D3gtk5Label12__ModuleInfoZ'
/tmp/ccxejYOB.o:(.data+0x40): undefined reference to `_D3gtk4Main12__ModuleInfoZ'
/tmp/ccxejYOB.o:(.data._D67TypeInfo_S3std8typecons35__T6scopedTC5cairo7Context7ContextZ6Scoped6__initZ[_D67TypeInfo_S3std8typecons35__T6scopedTC5cairo7Context7ContextZ6Scoped6__initZ]+0x58): undefined reference to `_D3std8typecons35__T6scopedTC5cairo7Context7ContextZ6Scoped6__dtorMFZv'
I have no clue what's going on here, and would appreciate all the help possible.
GtkD in the AUR is compiled using DMD. ABI compatibility between DMD and GDC binaries is not guaranteed and linking may fail as in your case. (Linking may also succeed and you get runtime problems). To troubleshoot the issue, you can try using DMD and see if that solves your issue.
Anyway, I would recommend using dub and gtk-d library from the dub registry. Dub will take care of the compilation of your source code and gtk-d's code and linking them together. And it will enable you to use any compiler easily.
If you don't want to use dub, you can also download gtk-d and build it yourself using GDC.