gpt4 book ai didi

linux - 在构建期间将静态库链接到共享库?

转载 作者:IT王子 更新时间:2023-10-29 00:23:46 26 4
gpt4 key购买 nike

我在使用 GCC/Linux 构建共享库时遇到问题。目前这个共享库是使用 GCC/libtool 选项“-shared”创建的,一切正常。

现在有两个额外的静态库(.a 文件)必须添加到这个共享库中,因为它们提供了共享库所需的一些功能。使用选项“-l”添加这些静态库没有帮助,之后它们不再是 .so 文件的一部分。

那么如何强制GCC/libtool将这些静态库的代码真正添加到共享库中呢?

谢谢!

最佳答案

在这种情况下,您需要 --whole-archive 链接器选项来命令链接器将整个静态库的内容包含到共享库中。

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

来自 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.

关于linux - 在构建期间将静态库链接到共享库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com