在构建期间将静态库链接到共享库? [英] Link a static library to a shared one during build?

查看:193
本文介绍了在构建期间将静态库链接到共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



那么我怎么能强制GCC / libtool将这些静态库的代码添加到共享库中?



感谢!

解决方案

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

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

man ld
$ b


对于在 - whole-archive 选项之后的命令行中提到的每个存档,请包含每个对象文件在链接的存档中,而不是在存档中搜索所需的目标文件。这通常用于将存档文件转换为共享库,从而强制每个对象都包含在生成的共享库中。这个选项可能会被多次使用。

从gcc使用这个选项时需要注意两点:首先,gcc不知道这个选项,所以你必须使用 - WL,-whole归档。其次,不要忘记在归档列表后面使用 -Wl,-no-whole-archive ,因为gcc会将自己的归档列表添加到链接中,并且您可能不希望此标志也影响到这些。



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 are two additional, static libraries (.a-files) that have to be added to this shared one since they provide some functionality that is required by the shared one. Adding these static libraries with option "-l" does not help, afterwards they are not part of the .so file.

So how can I force GCC/libtool to really add the code of these static libraries to the shared library?

Thanks!

解决方案

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.

这篇关于在构建期间将静态库链接到共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆