How to link static libs (archives) to an empty dynamic lib with GCC4
I'm using GCC on Linux to create a shared library. The library itself has no code开发者_开发知识库 but links to a couple of static libraries (archives) also built using GCC.
I need to export the static libs' symbols through my shared library. What happens is that the resulting shared lib is too small and it actually does not contain any of the symbols provided by the static libs mentioned above.
I also tried with a map of exported symbols but that did not help at all.
You need the linker's --whole-archive
option to pull in all of the static archives:
gcc -shared -o libwhatever.so -Wl,--whole-archive -lstatic -Wl,--no-whole-archive
The -Wl
is needed because --whole-archive
is a linker option.
In order to do this, the code in libstatic.a will need to have been properly compiled for use in a shared object (i.e. with -fpic
on platforms that require that).
精彩评论