ar 在现有的 .a 文件上?

发布于 2024-10-06 00:51:36 字数 327 浏览 0 评论 0原文

本质上,我想做的是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぽ尐不点ル 2024-10-13 00:51:36

实际上,您不想将整个“.a”文件归档到另一个文件中。您可能希望将一个存档的成员存档到另一个存档中 - 但这是完全不同的操作。

'ar' 程序完全能够在 '.a' 中存储源文件、gzip 压缩的 tar 文件、HTML 文件、XML 文件以及几乎任何其他类型的文件(并且 '.a' 后缀只是常规的,而不是强制)——尝试一段时间。但是,它会特殊对待目标文件(并且仅是目标文件),并使它们可供链接器(“ld”)使用,但链接器仅在扩展名是“.a”时才使用此类文件。

因此,您需要两个库:

  1. foo.a 仅包含 foo.o
  2. bar.a 包含 bar.o code> 以及 foo.a 中的所有对象

缺少按照您建议的操作并从 foo.a 中提取对象并将它们构建到 bar.a,唯一的选择是将 foo.a 的成员也列为 bar.a 的成员:

FOO_OBJS = foo.o
BAR_OBJS = bar.o $(FOO_OBJS)

foo.a: $(FOO_OBJS)
    $(AR) $(ARFLAGS) $@ $(FOO_OBJS)
bar.a: $(BAR_OBJS)
    $(AR) $(ARFLAGS) $@ $(BAR_OBJS)

我假设您的示例已最小化。如果不是,那么为什么要费心使用两个库呢?任何仅使用 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:

  1. foo.a containing just foo.o
  2. bar.a containing bar.o and all the objects from foo.a

Short of doing as you suggest and extracting the objects from foo.a and building them into bar.a, the only alternative is to list the members of foo.a as members of bar.a too:

FOO_OBJS = foo.o
BAR_OBJS = bar.o $(FOO_OBJS)

foo.a: $(FOO_OBJS)
    $(AR) $(ARFLAGS) $@ $(FOO_OBJS)
bar.a: $(BAR_OBJS)
    $(AR) $(ARFLAGS) $@ $(BAR_OBJS)

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.)

回忆凄美了谁 2024-10-13 00:51:36
ar rcs foo.a foo.o
cp foo.a foo2.a
gcc -c bar.c
ar rs foo2.a bar.o

如果您不需要保留其原始形式,则可以使用 foo.a 而不是 foo2.a

ar rcs foo.a foo.o
cp foo.a foo2.a
gcc -c bar.c
ar rs foo2.a bar.o

And perhaps use foo.a instead of foo2.a if you don't need to keep it in its original form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文