如何将多个 C/C++ 库合二为一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine several C/C++ libraries into one?
提问by GhassanPL
I'm tired of adding ten link libraries into my project, or requiring eight of them to use my own. I'd like to take existing libraries like libpng.a, libz.a, libjpeg.a, and combine them into one single .a library. Is that possible? How about combining .lib libraries?
我厌倦了在我的项目中添加十个链接库,或者需要其中八个使用我自己的。我想采用现有的库,如 libpng.a、libz.a、libjpeg.a,并将它们组合成一个单一的 .a 库。那可能吗?结合 .lib 库怎么样?
采纳答案by Avner
On Unix like systems, the ld and ar utilities can do this. Check out http://en.wikipedia.org/wiki/Ar_(Unix)or lookup the man pages on any linux box or through google, e.g 'unix man ar'.
在类 Unix 系统上,ld 和 ar 实用程序可以做到这一点。查看http://en.wikipedia.org/wiki/Ar_(Unix)或在任何 linux 机器上或通过谷歌查找手册页,例如“unix man ar”。
Please note that you might be better off linking to a shared (dynamic) library. This would add a dependency to your executable but will dramatically reduce its size, especially if you're writing a graphic application.
请注意,链接到共享(动态)库可能会更好。这会增加对可执行文件的依赖,但会显着减小其大小,尤其是在编写图形应用程序时。
回答by Judge Maygarden
You could extract the object files from each library with
您可以从每个库中提取目标文件
ar x <library name>
and then merge them all into a new library with
然后将它们全部合并到一个新库中
ar cs <new library name> <list each extracted object file>
回答by Star Brilliant
On Linux or MinGW or Cygwin, with GNU toolchain:
在 Linux 或 MinGW 或 Cygwin 上,使用 GNU 工具链:
ar -M <<EOM
CREATE libab.a
ADDLIB liba.a
ADDLIB libb.a
SAVE
END
EOM
ranlib libab.a
Or if you can keep the existence of liba.a
and libb.a
:
或者,如果你能保持的存在liba.a
和libb.a
:
ar crsT libab.a liba.a libb.a
On Windows, with MSVC toolchain:
在 Windows 上,使用 MSVC 工具链:
lib.exe /OUT:libab.lib liba.lib libb.lib
回答by Greg Whitfield
Maybe I'm misunderstanding, but don't you only have to ship the libs if the end-user code calls them directly? If all the access to Jpeg methods etc is from your code in your static library, then just link the libs into your lib.
也许我误解了,但是如果最终用户代码直接调用它们,您是否只需要发送库?如果对 Jpeg 方法等的所有访问都来自静态库中的代码,那么只需将库链接到您的库中。
I.e.
IE
----------------
| End-user exe |
----------------
|
| makes calls to
|
v
--------------------
| Your static lib.a |
--------------------
| makes calls to and links
v
------------------------------------ .....
| | |
------------- -------- ----------
| libjpeg.a | |libz.a| |libpng.a|
------------- -------- ----------
I.e it's only an issue if end code needs to make direct calls into libz.a, libpng.a etc.
也就是说,如果最终代码需要直接调用 libz.a、libpng.a 等,这只是一个问题。
If the app code has a legitimate need to call libz.a, for example, then that would as mentioned above be a case for using a dynamic module.
例如,如果应用程序代码合法需要调用 libz.a,那么如上所述,这就是使用动态模块的情况。
PS: Do I get an artists badge? :)
PS:我有艺术家徽章吗?:)
回答by Ed.
I'm not sure how to physically combine them into a single file, however you could employ an abstraction of a sort and just include a single "AllMyLibs.a/h" which in turn includes everything you want. You could also put this into the location where your compiler searches for libraries, so it would work for any project.
我不确定如何将它们物理地组合成一个文件,但是您可以采用某种抽象并只包含一个“AllMyLibs.a/h”,而后者又包含您想要的所有内容。你也可以把它放在你的编译器搜索库的位置,所以它适用于任何项目。
P.S. - Out of curiosity, why do you dislike including single libs?
PS - 出于好奇,你为什么不喜欢包含单个库?
回答by tuxedo
Combining several third-party libraries into one could create more problems for you--for instance, if two of those libraries define a common symbol which your program doesn't use. Now you've got to extract all (or all-but-one) of the instances of the common symbol before you combine the libraries.
将多个第三方库合并为一个可能会给您带来更多问题——例如,如果其中两个库定义了您的程序不使用的公共符号。现在,您必须在组合库之前提取所有(或全部)公共符号的实例。