Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force symbols from a static library to be included in a shared library build?

Tags:

I'm trying to build a shared object library that will be opened by a program using dlopen(). This library will use functionality provided by a separate library that is static.

I have included the appropriate flag on the link line to pull in the static library when linking the dynamic one (e.g. I have -lfoo for libfoo.a), and the linker doesn't complain. However, when the main program calls dlopen() on the dynamic library, the call fails with an "undefined symbol" message referencing a symbol from the static library.

Running nm does indicate that the symbol in question is undefined in the dynamic library, and the main program doesn't contain it, so how can I force the linker to pull this symbol in? The symbol itself is in the uninitialized data section (symbol type "B" in the nm output).

like image 302
BD at Rivenhill Avatar asked Aug 31 '11 07:08

BD at Rivenhill


People also ask

Do static libraries have symbols?

A .o file inside a library might contain symbols (functions, variables etc.) that are not used by your program. At link time, a static library can have unresolved symbols in it, as long as you don't need the unresolved symbols, and you don't need any symbol that is in a .o file that contains an unresolved symbol.

What is the difference between static library and shared library?

Static libraries take longer to execute, because loading into the memory happens every time while executing. While Shared libraries are faster because shared library code is already in the memory. In Static library no compatibility issue has been observed.


2 Answers

The --whole-archive linker option should do this. You'd use it as e.g.

gcc -o libmyshared.so foo.o -lanothersharedlib -Wl,--whole-archive -lmystaticlib 

What you're experiencing is that by default, the linker will search for symbols in a static archive that the binary you produce needs, and if it needs one, it'll include the whole .o that the symbol resides in. If your shared library doesn't need any of the symbols, they will not be included in your shared lib.

Remember that code that becomes a shared library needs to be compiled with special options, such as -fpic , as you're including a static library in your shared library, the static library needs to be compiled with the same options.

like image 50
nos Avatar answered Oct 26 '22 07:10

nos


Recently I was searching solution for the same. I found using

--undefined=symbol 

or

-u symbol 

solves the problem.

like image 44
itdl Avatar answered Oct 26 '22 05:10

itdl