Why does compiling a shared object with GCC always result in undefined references to main?

You forgot the -shared option in your gcc link command line.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm running Solaris, so it's possible that this is specific to running GCC on Solaris. If I use GCC to generate a shared object, and then run nm on it to see undefined symbols, there will always be a reference to main: 624 | 0| 0|NOTY |GLOB |0 |UNDEF |main If I manually generate the same shared object using ld, the reference to main doesn't exist. If I run nm on the system libraries in /usr/lib, none of them appear to have references to main.

Only shared libraries I compile myself with GCC. Apps compiled against these shared libraries work fine and without errors. But I still don't understand why the reference to main is there in the first place.

Any clues? Gcc linker shared-libraries symbols link|improve this question asked Oct 16 '09 at 17:23Joseph Garvin3,9831950 61% accept rate.

– Pierre Bourdon Oct 16 '09 at 17:32 To expand on delroth's comment, you are most likely leaving out some flag that GCC should pass to the loader to tell it to just create a shared lib and not try to link against libc. If I recall correctly, invoking gcc with the -v flag will make it print the exact commands it's executing, and will show the flags it's giving ld. You can compare that with your own ld command and it should become clear.

– Berry Oct 16 '09 at 17:42 I don't think there's anything exotic in here, and it has the -G to indicate creation of a shared library: g++ -Wl,-R/export/home/joeg/fresh/lib -L/opt/tradelink/g++lib6/lib -Wl,-R/opt/tradelink/g++lib6/lib -L/opt/app/g++lib6/toast-1.1/lib -L/opt/app/g++lib6/boost-1.34/lib -Wl,-G -fpic -o libprice.so *. Fpo -Wl,-Bstatic -ltoast_datetime -ltoast_assert -ltoast_typeinfo -ltoast_async -lboost_filesystem-gcc42-mt -lboost_date_time-gcc42-mt -lboost_regex-gcc42-mt -lboost_signals-gcc42-mt -lboost_thread-gcc42-mt -lboost_program_options-gcc42-mt -Wl,-Bdynamic -lrt -lpthread – Joseph Garvin Oct 16 '09 at 18:12.

You forgot the -shared option in your gcc link command line. EDIT: and you forgot -fPIC option on your compile command line (which is causing all the relocation errors at link time). If you still get relocation errors with -fPIC on all compile lines, then you should rebuild all the archive libraries which you link in (libtoast_datetime, libtoast_assert, etc.) with -fPIC as well.

Anyway, trying with "-shared" I get a million linker errors about Unwind_resume missing in boost. If I try again with "-shared-libgcc" then I get no errors, but the . So has a reference to main again O_o – Joseph Garvin Oct 16 '09 at 18:57 Also strangely they're text relocation errors, not undefined reference errors.

– Joseph Garvin Oct 16 '09 at 19:24 It turns out you were right, my testing just wasn't working because I don't write the link line myself, my make tool does, so I'd been copy and pasting the link line it'd generated and tweaking it, and I accidentally was using a link line from a shared build in a static sandbox ;p Separate question -- why has this worked in the past? All the shared objects here have been built with -fPIC in the past, but not with -shared and have worked fine. This didn't become an issue until I started work to make linking more precise (only pulling in exactly what's needed).

– Joseph Garvin Oct 17 '09 at 18:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions