Shawn
Shawn

Reputation: 1471

How to add specific symbols from static library

I am trying to use --whole-archive option, but it seems to not work with -Xlinker. How can I add this option by using -Xlinker ?

Here is the whole story: I write a main program A, and several shared libraries B1,B2,etc. main program will use dlopen to load these shared libraries at runtime. I compile main program A linking with ALL the possible static libraries used by main program (S1,S2...etc) or shared libraries (S3,S4...etc). then I compile shared library without static library. When calling dlopen, the program fail because of undefined symbol in shared library. this symbol does exist in static library S3, but I can not find it in symbol table of main program.

So I add --whole-archive option when compiling main program, but the symbol still not exists in final binary. how can I add this symbol to main program while it is not direct used in main program.

Upvotes: 0

Views: 924

Answers (1)

Employed Russian
Employed Russian

Reputation: 213446

So I add --whole-archive option when compiling main program

You still have not answered the question: what command line you used. Adding linker options when compiling in generally pointless: they only matter when linking.

The correct link line for the situation you described is (assuming you are using GCC on a UNIX/ELF system):

 gcc -rdynamic -o exename main.c \
   -Wl,--whole-archive -lS1 -lS2 -Wl,--no-whole-archive

Explanation of important points:

  1. The -rdynamic is needed so that symbols defined in the main executable are visible to dlopened shared libraries.
  2. Libraries should follow sources on the link line (order of libraries and sources matters).
  3. Libraries which you want to link in entirely should be surrounded by --whole-archive and --no-whole-archive options.

Upvotes: 1

Related Questions