c++ error while compiling with both clang++ and g++ - c++

I am working on a project which modifies the control flow of user program. I am using llvm for this. At the end I generate a set of .s files using clang++. Then I use g++ to merge them all. I cant use the clang++ (instead of g++) as its giving a lot of errors at this stage (this is not the current problem)
For c programs (gcc and clang), things are working perfectly. But for c++ user code (g++, clang++) I am getting the following error when I execute
g++ -O3 -rdynamic *.s -o main.exe -Wa,-Iressource -lstdc++ -lm -ldl
/tmp/ccUdIbL3.o:(.ctors+0x0): undefined reference to `global constructors keyed to a'
/tmp/cch9k7on.o: In function `global constructors keyed to a':
_GLOBAL__I_a.optin:(.text.startup+0x1): undefined reference to `__cxx_global_var_init'
/tmp/ccvYrG8J.o:(.ctors+0x0): undefined reference to `global constructors keyed to a'
/tmp/cccPFs4G.o:(.ctors+0x0): undefined reference to `global constructors keyed to a'
As per my search on internet, for most of the people this error occurred because they use clang (instead of clang++).
Clang
clang --version
clang version 3.1 (branches/release_31)
Target: x86_64-unknown-linux-gnu
Thread model: posix
g++
g++ --version
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
I have no idea of where the actual problem is. But I know that if I simply use clang++ to generate *.s files and then use g++ (without my llvm pass) it works fine. Since it llvm dont complain till the end then g++ does, I dont know where the actual error occurred. As I said earlier if the original code was "C" (by using clang and gcc) it works fine.
Any help on how to proceed or a solution to solve the problem is appreciated.
Specially what might lead to (in this context)
undefined reference to __cxx_global_var_init
global constructors keyed to a

Related

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.

Finding mangled C++ link error

There are two link errors in this project.
The first, pointed out by #Steve, is the lack of
void *__gxx_personality_v0;
If anyone can explain why a C++ project compiled with clang++3.5 under cygwin would create a reference to this, I would like to understand. Anyway, I defined the symbol as a workaround.
The other problem appears to be references to 32-bit relative addressing in 64-bit. I am looking for a way to tell clang++ to compile 32 bit or 64 bit. I will try on native linux as well, but again, if anyone can explain why this is happening, I would like to understand.
I am thinking it might be a static variable, located down low in the address space, being accessed by something on the stack, but again, clang is generating everything so as far as I can see, this is a bug in the compiler.
clang++ -std=c++11 -O2 -g -I include/ -pthread -c RequestHandler.cc
clang++ -std=c++11 -O2 -g Config.o Logger.o RequestHandler.o FileSys.\
o Buffer.o server.o CspServlet.o -pthread -o server
RequestHandler.o:fake:(.debug_info+0x54bd): relocation truncated to f\
it: R_X86_64_32 against `.debug_ranges'
...
FileSys.o:fake:(.eh_frame$_ZNSt10_HashtableISsSt4pairIKSsP2FLESaIS4_E\
NSt8__detail10_Select1stESt8equal_toISsESt4hashISsENS6_18_Mod_range_h\
ashingENS6_20_Default_ranged_hashENS6_20_Prime_rehash_policyENS6_17_H\
ashtable_traitsILb1ELb0ELb1EEEE21_M_insert_unique_nodeEmmPNS6_10_Hash\
_nodeIS4_Lb1EEE+0x13): undefined reference to `__gxx_personality_v0'
FileSys.o:fake:(.eh_frame$_ZNSt8__detail16_Hashtable_allocISaINS_10_H\
ash_nodeISt4pairIKSsP2FLELb1EEEEE16_M_allocate_nodeIJRKS6_EEEPS7_DpOT\
_+0x13): more undefined references to `__gxx_personality_v0' follow
collect2: error: ld returned 1 exit status
Ok, it turns out that on cygwin, when I installed clang, it doesn't fully use the clang toolchain. clang is compiling, but g++ is linking (I think). Bear in mind all these tools could be aliased to something else, but it's very suspicious.
I built on Unix and it worked. Same makefile.
So the problem is that when I wrote clang, it wasn't using clang to link. And that is weird.

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.

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!