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

时间:2022-09-18 12:22:17

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.

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

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.

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

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

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

Thanks!

谢谢!

2 个解决方案

#1


21  

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

您需要——在本例中,全存档链接器选项可以命令链接器将整个静态libs的内容包含到共享库中。

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

From man ld:

从男人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.

当使用gcc的这个选项时,有两个注释:首先,gcc不知道这个选项,所以您必须使用-Wl,- all -archive。其次,不要忘记在归档列表之后使用-Wl,-no-whole archive,因为gcc将在您的链接中添加它自己的归档列表,您可能不希望这个标记也影响这些归档。

#2


2  

You only need the --whole-archive parameter to force the linker to include the library, but it should be able to infer its own needs from unmatched symbols.

您只需要- all -archive参数来强制链接器包含库,但是它应该能够从不匹配的符号推断出它自己的需求。

Ensure that any static libraries on the command-line come after their dependent object-files e.g.:

确保命令行上的任何静态库都位于它们所依赖的对象文件之后,例如:

g++ -Wl,-E -g -pipe -O2 -pipe -fPIC  myobjectfile.o mystaticlibrary.a -shared -o mylib.so

#1


21  

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

您需要——在本例中,全存档链接器选项可以命令链接器将整个静态libs的内容包含到共享库中。

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

From man ld:

从男人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.

当使用gcc的这个选项时,有两个注释:首先,gcc不知道这个选项,所以您必须使用-Wl,- all -archive。其次,不要忘记在归档列表之后使用-Wl,-no-whole archive,因为gcc将在您的链接中添加它自己的归档列表,您可能不希望这个标记也影响这些归档。

#2


2  

You only need the --whole-archive parameter to force the linker to include the library, but it should be able to infer its own needs from unmatched symbols.

您只需要- all -archive参数来强制链接器包含库,但是它应该能够从不匹配的符号推断出它自己的需求。

Ensure that any static libraries on the command-line come after their dependent object-files e.g.:

确保命令行上的任何静态库都位于它们所依赖的对象文件之后,例如:

g++ -Wl,-E -g -pipe -O2 -pipe -fPIC  myobjectfile.o mystaticlibrary.a -shared -o mylib.so