Link a static library to a shared one during build?

后端 未结 2 1468
無奈伤痛
無奈伤痛 2021-02-02 13:45

I have a problem building a shared library with GCC/Linux. Currently this shared library is created with GCC/libtool option \"-shared\" and everything is fine.

Now there

2条回答
  •  孤独总比滥情好
    2021-02-02 14:09

    You need --whole-archive linker option in this case to command the linker to include whole static libs' content into the shared lib.

    g++ -shared sample.o -o libSample.so -Wl,-whole-archive -lmylib1.a -lmylib2.a -Wl,-no-whole-archive
    

    From man ld:

    For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

    Two notes when using this option from gcc: First, gcc doesn't know about this option, so you have to use -Wl,-whole-archive. Second, don't forget to use -Wl,-no-whole-archive after your list of archives, because gcc will add its own list of archives to your link and you may not want this flag to affect those as well.

提交回复
热议问题