Ld complains: in function “_start”, undefined reference to “__libc_csu_fini” & “__libc_csu_init”

cgtkld

I'm compiled a GTK+ program using clang, rather than gcc. So far so good, except I can't have ld link correctly.

Original makefile called gcc to do the linking, like this:

gcc -g -O2 -export-dynamic -o my_application somefile.o another.o -pthread -lgnomeui-2 -lSM -lICE -lbonoboui-2 -lgnomevfs-2 -lgnomecanvas-2 -lgnome-2 -lpopt -lbonobo-2 -lbonobo-activation -lORBit-2 -lart_lgpl_2 -lgconf-2 -lglade-2.0 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0 -lxml2 -pthread -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lxml2 -lgthread-2.0 -lrt -lglib-2.0

If I simply substitute gcc with clang in the above command, I get this warning from the latter:

clang: warning: argument unused during compilation: '-e xport-dynamic'

Problem is: clang doesn't recognize "-export-dynamic" as a linker option, so it doesn't pass it off to ld. This causes the references to external functions to be scrapped: when final binary is executed, neither the menu options nor the buttons react.

If I do final linking step with gcc the binary works perfectly (even though it was really compiled with clang and gcc only does the linking). Since I'm trying to completely replace gcc with clang in my machine, this is not acceptable.

So I tried this:

ld /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o -g -O2 -export-dynamic my_application somefile.o another.o -o -lgnomeui-2 -lSM -lICE -lbonoboui-2 -lgnomevfs-2 -lgnomecanvas-2 -lgnome-2 -lpopt -lbonobo-2 -lbonobo-activation -lORBit-2 -lart_lgpl_2 -lgconf-2 -lglade-2.0 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0 -lxml2 -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lxml2 -lgthread-2.0 -lrt -lglib-2.0

By adding first 3 objects I avoid the "can't find _start" message. But then I get the aforementioned two complaints:

/usr/lib/crt1.o: In function _start:
(.text+0x12): undefined reference to __libc_csu_fini
/usr/lib/crt1.o: In function _start:
(.text+0x19): undefined reference to __libc_csu_init

I tried adding crtbegin.o and crtend.o at the list, even before crt1.o , to no avail. What is the linker's correct invocation?

(Post Edit): If nothing else works, is there a way to catch what exact parameters gcc is passing to ld when gcc is used for the linking?

Best Answer

gcc automatically adds language support libraries and objects to your supplied list for link. You have to add them yourself if you want to invoke ld directly. Taking an example from man ld, it could be

ld -o <output> /lib/crt0.o hello.o -lc

Try adding -lc, the symbol you need should be in it (at least on my system, it is).

Related Topic