user360607
user360607

Reputation: 373

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.

Upvotes: 0

Views: 128

Answers (1)

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76531

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).

Upvotes: 3

Related Questions