将静态库链接到其他静态库 [英] Linking static libraries to other static libraries

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

问题描述

我有一小段代码,依赖于许多静态库(a_1-a_n)。我想把这个代码封装在一个静态库中,并让其他人使用。

I have a small piece of code that depends on many static libraries (a_1-a_n). I'd like to package up that code in a static library and make it available to other people.

我的静态库,可以称为X,编译得很好。

My static library, lets call it X, compiles fine.

我创建了一个简单的示例程序使用X的函数,但是当我尝试将它链接到X,我得到许多关于从库a_1 - a_n缺少符号的错误。

I've created a simple sample program that uses a function from X, but when I try to link it to X, I get many errors about missing symbols from libraries a_1 - a_n.

有没有办法我可以创建一个新的静态库,Y包含X和X所需的所有功能(从a_1 - a_n选择的位),以便我可以分发只有Y,人们链接他们的程序?

Is there a way that I can create a new static library, Y that contains X and all the functionality needed by X (selected bits from a_1 - a_n), so that I can distribute just Y for people to link their programs to?

更新:

/ strong>,并制作一个大型库,然而,最终包括了大量的符号,不需要(所有的.o文件是大约700 MB,然而,一个静态链接的可执行文件是7 MB)。

I've looked at just dumping everything with ar and making one mega-lib, however, that ends up including a lot of symbols that are not needed (all the .o files are about 700 MB, however, a statically linked executable is 7 MB). Is there a nice way to include only what is actually needed?

这看起来与

This looks closely related to How to combine several C/C++ libraries into one?.

推荐答案

静态库不与其他静态库链接。执行此操作的唯一方法是使用库管理器/存档器工具(例如Linux上的 ar )通过连接多个库来创建一个新的静态库。

Static libraries do not link with other static libraries. The only way to do this is to use your librarian/archiver tool (for example ar on Linux) to create a single new static library by concatenating the multiple libraries.

编辑:为了响应您的更新,我知道只选择所需的符号的唯一方法是从.o文件的子集手动创建包含他们。这是困难,耗时和容易出错。我不知道任何工具来帮助做这个(不是说他们不存在),但它会使一个有趣的项目,生产一个。

In response to your update, the only way I know to select only the symbols that are required is to manually create the library from the subset of the .o files that contain them. This is difficult, time consuming and error prone. I'm not aware of any tools to help do this (not to say they don't exist), but it would make quite an interesting project to produce one.

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

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