Link a static library to a shared one during build?
Asked Answered
I

2

20

I have a problem building a shared library with GCC/Linux. Currently this shared library is created with GCC/libtool option "-shared" and everything is fine.

Now there are two additional, static libraries (.a-files) that have to be added to this shared one since they provide some functionality that is required by the shared one. Adding these static libraries with option "-l" does not help, afterwards they are not part of the .so file.

So how can I force GCC/libtool to really add the code of these static libraries to the shared library?

Thanks!

Ignoble answered 15/2, 2013 at 7:22 Comment(6)
For creating executables you can just add the full path of the static library to the building options of gcc, such as: gcc -Wall -g live_mystuff.c -o my_app /usr/local/src/ffmpeg/libswscale/libswscale.a -lpthread -lbz2 -lm -lz -lfaac -lmp3lame -lx264 -lfaad ... It might work also for creating shared libraries. Try it.Cyton
How do you know they are "not part of the .so file"?Hexose
@fritzone: for executables that works without problems, I have to do it for a shared libraryIgnoble
@Joachim: for two reasons: the shared library does not become bigger and when linking an executable against it, it complains about missing functions - the functions, that reside within the static librariesIgnoble
How do you link the shared library? Please show complete command line, or makefile output.Hexose
@Joachim: Currently I build it using libttool: libtool --mode=compile g++ -Wall -fPIC -shared -Wno-unused -D_REENTRANT and libtool --mode=link g++ -shared -rpath /usr/lib Now when I add a -lmystaticlib to one/both statements it seems to be ignored...Ignoble
A
28

You need --whole-archive linker option in this case to command the linker to include whole static libs' content into the shared lib.

g++ -shared sample.o -o libSample.so -Wl,-whole-archive -lmylib1.a -lmylib2.a -Wl,-no-whole-archive

From man ld:

For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

Two notes when using this option from gcc: First, gcc doesn't know about this option, so you have to use -Wl,-whole-archive. Second, don't forget to use -Wl,-no-whole-archive after your list of archives, because gcc will add its own list of archives to your link and you may not want this flag to affect those as well.

Airship answered 15/2, 2013 at 8:22 Comment(1)
on SunOS I had to use bot dashes after -WL,.Unknot
H
3

You only need the --whole-archive parameter to force the linker to include the library, but it should be able to infer its own needs from unmatched symbols.

Ensure that any static libraries on the command-line come after their dependent object-files e.g.:

g++ -Wl,-E -g -pipe -O2 -pipe -fPIC  myobjectfile.o mystaticlibrary.a -shared -o mylib.so
Henslowe answered 30/4, 2015 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.