Linking object code with ld gives multiple errors about missing shared libraries or symbols
Environment
-
Red Hat Enterprise Linux 6
-
Red Hat Enterprise Linux 5
- Red Hat Enterprise Linux 4
- gcc
- binutils (ld)
Issue
- A simple program is built to get object code using the following command:
g++ -c sample.cc -o sample.o
- Linking the object code sample.o with ld gives multiple errors about missing shared libraries or symbols:
$ ld sample.o -o sample
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048094
sample.o: In function_\_static\_initialization\_and\_destruction_0(int, int)':std::ios_base::Init::Init()'
sample.cc:(.text+0x23): undefined reference to
sample.cc:(.text+0x2b): undefined reference to_\_dso\_handle'__cxa_atexit'
sample.cc:(.text+0x3f): undefined reference to
sample.o: In function_\_tcf\_0':std::ios_base::Init::~Init()'
sample.cc:(.text+0x6c): undefined reference to
sample.o: In functionmain':std::cout'
sample.cc:(.text+0x8e): undefined reference to
sample.cc:(.text+0x93): undefined reference tostd::basic\_ostream<char, std::char\_traits<char> >& std::operator<< <std::char\_traits<char> >(std::basic\_ostream<char, std::char_traits<char> >&, char const\*)'std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
sample.cc:(.text+0x9b): undefined reference to
sample.cc:(.text+0xa3): undefined reference tostd::ostream::operator<<(std::ostream& (\*)(std::ostream&))'__gxx_personality_v0'
sample.o:(.eh\_frame+0x11): undefined reference to
Resolution
- This is expected behaviour. To link the object code into an executable, use g++ itself:
g++ -o sample sample.o
- Alternatively, to use ld, the following command will work on i386:
ld --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o sample \
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../lib/crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../lib/crti.o \
/usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 \
-L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib sample.o -lstdc++ -lm -lgcc_s -lgcc -lc \
-lgcc_s -lgcc /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../lib/crtn.o
- Avoid using ld in the manner above unless absolutely necessary since the parameters in the above command work only on i386
Root Cause
- gcc automatically adds all of the above linker parameters and passes them to ld. Invoking ld directly requires the user to manually provide all these arguments separately for each architecture.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
