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

前端 未结 3 1429
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  • 2020-12-08 05:44

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

    --undefined=symbol
    

    or

    -u symbol
    

    solves the problem.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 06:03

    Another hack is to take the address of the function somewhere during initialization of the library. This will make sure you actually use the symbol.

    0 讨论(0)
提交回复
热议问题