GCC undefined references with abi:cxx11 - c++

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.

Related

Symbol not found by linker but correct file present [duplicate]

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.

Can't link against static library with Mingw on Linux

I have installed the GMP library and try to cross-compile with mingw-w64-posix.
My Library is in /usr/local/lib.
My compile command looks like the following:
x86_64-w64-mingw32-g++-posix src/factorial.cpp -o bin/factorial.win.o -I/usr/local/include -L/usr/local/lib -lgmp -lgmpxx
It throws an undefined reference error:
(I can remove the whole block from -L...., same error. Seems like the library doesnt link for some reason)
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3_[_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3_]+0x27): undefined reference to `__gmpz_mul'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_l[_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_l]+0x26): undefined reference to `__gmpz_mul_si'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_E7init_siEl[_ZN10__gmp_exprIA1_12__mpz_structS1_E7init_siEl]+0x1a): undefined reference to `__gmpz_init_set_si'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_EC1EOS2_[_ZN10__gmp_exprIA1_12__mpz_structS1_EC1EOS2_]+0x2e): undefined reference to `__gmpz_init'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_ED1Ev[_ZN10__gmp_exprIA1_12__mpz_structS1_ED1Ev]+0x14): undefined reference to `__gmpz_clear'
collect2: error: ld returned 1 exit status
However if i change my compiler to g++ instead everything works fine.
OK -
The link errors (__gmpz_init, __gmpz_clear, etc.) are GMP "internals". They're supposed to come from libgmp, the C-language base library.
The code that's referencing them (.text$ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3[ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3], etc.) is "name mangled" C++.
I suspect the problem is that your "gmpxx" library was built with a different C++ compiler, that has a different "name mangling" convention than MinGW.
SOLUTION:
Download the complete libGMP source (e.g. from https://gmplib.org/, and rebuild EVERYTHING (including libgmpxx) with your libmingw-w64-posix++ C++ cross-compiler.
ADDENDUM:
I downloaded gmp-6.2.1 source, and found __gmpz_clear here:
gmp-6.2.1\gmp-h.in
#define mpz_clear __gmpz_clear
__GMP_DECLSPEC void mpz_clear (mpz_ptr);
"gmp-h.in" is a template used by the project's "autoconf", to generate the libGMP source files for the specified target environment.
Which, in turn, means:
The project you started out with (in your original question) wasn't configured for MinGW
... and ...
You didn't run "configure" correctly when you tried building from source.
SUGGESTION:
Try building libGMP from source again. DELETE everything, re-extract from the libGMP tarball, and carefully follow the INSTALL instructions:
./configure
make
make check <= VERY IMPORTANT!!
make install
I'm curious about your build environment (Windows? Linux?), compiler (exact MinGW version) and target (if you're building on a Windows workstation, do you want to run your GMP app as a Windows .exe)?

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.

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.

Compiling with Clang using Libc++ undefined references

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!