1

I'm really confused here, here's what I think is a simplified model of my situation:

libMyLibrary2.a has references to libMyLibrary1.a. If I link like this:

gcc -shared dummy.cpp.o -o libOutput.so -L/path/to/libs -lMyLibrary1 -lMyLibrary2

I get a whole bunch of undefined reference to errors. But if I link like this:

gcc -shared dummy.cpp.o -o libOutput.so -L/path/to/libs -lMyLibrary1 /path/to/libs/libMyLibrary2.a

Everything's okay. Is this normal? Or should it have something to do with my specific settings? In reality I have numerous libraries in place of MyLibrary1 and MyLibrary2 and also many flags. Or is it the case that I'm missing something else, maybe some key difference between the commands? I can post the exact commands that do and don't work if required.

Ayberk Özgür
  • 4,986
  • 4
  • 38
  • 58

1 Answers1

2

You need to tell the linker to link the whole archive files:

gcc -shared dummy.cpp.o -o libOutput.so -L/path/to/libs -Wl,-whole-archive -lMyLibrary1.a -lMyLibrary2.a -Wl,-no-whole-archive

As static libraries have no dependencies, you need to tell the linker to create a new static library, that contains both sets of objects.

Your second case (which works fine) has the desired effect, because you pass the static library directly to your compiler.

In the first case, the linker will take the objects from library1 and library2 that are referenced by the dummy object.

Jens Luedicke
  • 964
  • 2
  • 8
  • 20
  • There is no such flag in both commands, one of which is working fine. – Ayberk Özgür Sep 17 '14 at 14:38
  • I see, this makes sense. This agrees with this as well: http://stackoverflow.com/questions/9952146/proper-way-to-link-a-static-library-using-gcc. So in the end, there is a difference between passing -lMyLib and libMyLib.a after all if there are dependencies to other linked libraries. – Ayberk Özgür Sep 18 '14 at 08:44