ar 在现有的 .a 文件上? [英] ar on an existing .a file?

查看:25
本文介绍了ar 在现有的 .a 文件上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我想做的是:

gcc foo.c -o foo.oar rcs foo.a foo.ogcc bar.c -o boo.oar rcs bar.a bar.o foo.a

我想将一个对象和一个静态库归档到另一个静态库中.不幸的是,最后一个命令最终没有包含 foo.o(它包含 bar.o 和 foo.a),所以当我进入链接阶段时,链接器无法从 foo.o 中找到符号.

有没有办法在这里做我想做的事?我正在做所有这些,所以我正在寻找一种不涉及提取 & 的解决方案.重新归档对象(这看起来有点痛苦).有没有办法让这种从另一个存档存档"设置工作?

解决方案

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

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

所以,你想要两个库:

  1. foo.a 只包含 foo.o
  2. bar.a 包含 bar.o 和来自 foo.a
  3. 的所有对象

没有按照您的建议执行并从 foo.a 中提取对象并将它们构建到 bar.a 中,唯一的选择是列出 foo.a 也是 bar.a 的成员:

FOO_OBJS = foo.oBAR_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 链接.(如果这些是共享对象会略有不同,但只使用一个而不是两个共享对象可能仍然更好.)

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?

解决方案

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

这篇关于ar 在现有的 .a 文件上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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