ar 在现有的 .a 文件上?
本质上,我想做的是:
gcc foo.c -o foo.o
ar rcs foo.a foo.o
gcc bar.c -o boo.o
ar rcs bar.a bar.o foo.a
我想将对象和静态库归档到另一个静态库中。不幸的是,最后一个命令最终并不包含 foo.o (它包含 bar.o 和 foo.a),因此当我进入链接阶段时,链接器无法找到 foo.o 中的符号。
有办法在这里做我想做的事吗?我正在做所有这些事情,所以我正在寻找一种不涉及提取和提取的解决方案。重新归档对象(这看起来有点痛苦)。有没有办法使这种“从另一个存档存档”设置起作用?
Essentially, what I want to do is this:
gcc foo.c -o foo.o
ar rcs foo.a foo.o
gcc bar.c -o boo.o
ar rcs bar.a bar.o foo.a
I want to archive both an object and a static library into another static library. Unfortunately, the last command doesn't end up containing foo.o (it contains bar.o and foo.a), so when I get to the linking stage, the linker can't find the symbols from foo.o.
Is there a way to do what I want here? I'm doing all of this out of make, so I'm looking for a solution that doesn't involve extracting & re-archiving the objects (which seems kinda painful). Is there a way to make this kind of "archive from another archive" setup work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您不想将整个“.a”文件归档到另一个文件中。您可能希望将一个存档的成员存档到另一个存档中 - 但这是完全不同的操作。
'ar' 程序完全能够在 '.a' 中存储源文件、gzip 压缩的 tar 文件、HTML 文件、XML 文件以及几乎任何其他类型的文件(并且 '.a' 后缀只是常规的,而不是强制)——尝试一段时间。但是,它会特殊对待目标文件(并且仅是目标文件),并使它们可供链接器(“ld”)使用,但链接器仅在扩展名是“.a”时才使用此类文件。
因此,您需要两个库:
foo.a
仅包含foo.o
bar.a
包含bar.o
code> 以及foo.a
中的所有对象缺少按照您建议的操作并从
foo.a
中提取对象并将它们构建到bar.a,唯一的选择是将
foo.a
的成员也列为bar.a
的成员:我假设您的示例已最小化。如果不是,那么为什么要费心使用两个库呢?任何仅使用 foo.o 中的代码的内容都只会链接 foo.o,即使给定了 bar.a 来链接。 (如果这些是共享对象,情况会略有不同,但仅使用一个而不是两个共享对象可能仍然更好。)
Actually, you do not want to archive one whole '.a' file inside another. You might want to archive the members of the one archive in the other - but that is a wholly different operation.
The 'ar' program is perfectly capable of storing source files, gzipped tar files, HTML files, XML files, and pretty much any other type of file in the '.a' (and the '.a' suffix is only conventional, not mandatory) -- try it some time. However, it treats object files (and only object files) specially, and makes them available for use by the linker ('ld'), but the linker only uses such files if the extension is '.a'.
So, you want to have two libraries:
foo.a
containing justfoo.o
bar.a
containingbar.o
and all the objects fromfoo.a
Short of doing as you suggest and extracting the objects from
foo.a
and building them intobar.a
, the only alternative is to list the members offoo.a
as members ofbar.a
too:I am assuming that your example is minimized. If it is not, then why bother with two libraries. Anything that uses just code from foo.o will only link foo.o even if given bar.a to link with. (It would be slightly different if these were shared objects, but it would probably still be better to use just one rather than two shared objects.)
如果您不需要保留其原始形式,则可以使用
foo.a
而不是foo2.a
。And perhaps use
foo.a
instead offoo2.a
if you don't need to keep it in its original form.