views:

90

answers:

1

Hello, I am creating an application, which consists of two static libs and an executable.

Let's call the two static libs: libusefulclass.a libcore.a

And the application: myapp

libcore instantiates and uses the class defined in libusefulclass (let's call it UsefulClass)

Now, if I link the application in the following way:

g++ -m64 -Wl,-rpath,/usr/local/Trolltech/Qt-4.5.4/lib -o myapp src1.o src2.o srcN.o -lusefulclass -lcore

The linker complains about the methods in libusefulclass not being found:

undefined reference to `UsefulClass::foo()'

etc.

I found a workaround for this: If UsefulClass is also instantiated within the source files of the executable itself, the application is linked without any problems.

My question is: is there a more clean way to make libcore refer to methods defined in libusefulclass, or static libs just cannot be linked against eachother?

TIA

P.S.: In case that matters: the application is being developed in C++ using Qt, but I feel this is not a Qt problem, but a library problem in general.

+3  A: 

You need to specify the libraries in reverse order of dependencies, so use

g++ -m64 -Wl,-rpath,/usr/local/Trolltech/Qt-4.5.4/lib -o myapp src1.o src2.o srcN.o  -lcore -lusefulclass

If there's a cyclic dependency, you might even need to specify the library twice,

g++ -m64 -Wl,-rpath,/usr/local/Trolltech/Qt-4.5.4/lib -o myapp src1.o src2.o srcN.o  -lusefulclass -lcore -lusefulclass
nos
Thank you, nos, this indeed fixed it.(Remark: for me it would seem more logical the other way round, i.e. that usefulclass preceedes core, if core depends on usefulclass... I'll keep in mind the above mentioned rule, though :)
andras
the linker reads the symbol from the archive and discards anything noone references yet, then the next library comes along and need symbols that the linker already discarded - so you'll have to provide them in the "reverse" order
nos