问题描述
基本上,我想要做的是这样的: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中找到符号。
有没有办法做到我想要的?我正在做所有这些,所以我正在寻找一种解决方案,不涉及提取&重新归档对象(这看起来有点痛苦)。有没有办法让这种从另一个档案归档设置工作?
其实,你不想将另一个整个'.a'文件存档。您可能想要将其中一个档案的成员归档到另一个档案中 - 但这是完全不同的操作。
'ar'程序完全能够存储源文件,压缩的tar文件,HTML文件,XML文件以及'.a'中的任何其他类型的文件(并且'.a'后缀只是常规的,不是强制的) - 尝试一段时间。但是,它专门处理目标文件(仅限于目标文件),并使其可供链接程序('ld')使用,但链接程序仅在扩展名为.a时才使用此类文件。
所以,你需要有两个库:
- foo.a 包含 foo.o
- bar.a 包含 bar.o 以及所有来自 foo.a
- foo.a containing just foo.o
- bar.a containing bar.o and all the objects from foo.a
当您建议并从 foo.a 中提取对象并将它们构建到中时, bar.a ,唯一的选择是将 foo.a 的成员列为 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)
我假设您的示例已最小化。如果不是,那么为什么还要打扰两个图书馆。任何只使用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:
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文件上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!