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

前端 未结 3 1442
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 05:00

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 stat

3条回答
  •  Happy的楠姐
    2020-12-08 05:57

    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.

提交回复
热议问题