Linux 如何将两个“ar”静态库合并为一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3821916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge two "ar" static libraries into one?
提问by osgx
I have 2 static Linux libraries, created by ar cr
, libabc.a
and libxyz.a
.
I want to merge them into one static library libaz.a
.
How can I do this.
我有 2 个静态 Linux 库,由ar cr
、libabc.a
和libxyz.a
.
我想将它们合并到一个静态库中libaz.a
。
我怎样才能做到这一点。
I want to create a merged static library, not to give both libraries to final link of applications.
我想创建一个合并的静态库,而不是将两个库都提供给应用程序的最终链接。
采纳答案by codaddict
You can extract the object from both the .a
files and create your .a
file using the extracted .o
s:
您可以从两个.a
文件中提取对象并.a
使用提取的.o
s创建文件:
ar -x libabc.a
ar -x libxyz.a
ar -c libaz.a *.o
回答by samuel.zzy220
If you simply do it as :
如果你只是这样做:
ar x a.a
ar x b.a
ar c c.a *.o
you will lost some object files if there are members with same name in both a.a and b.a so, you need to extract members of different archives into different folder:
如果 aa 和 ba 中有相同名称的成员,您将丢失一些目标文件,因此您需要将不同档案的成员提取到不同的文件夹中:
ar x a.a && mv *.o a_objs
ar x b.a && mv *.o b_objs
ar c c.a a_objs/*.o b_objs/*.o
further more, it is posible that there are multiple members of same name in one archive (say in a.a), if you run ar x a.a, you will get only one for those members of same name.
此外,一个档案中可能有多个同名成员(例如在 aa 中),如果您运行ar x aa,则这些同名成员只会得到一个。
The only way to extract all members of same name in one archive is to specify the member number by option 'N':
在一个档案中提取所有同名成员的唯一方法是通过选项“N”指定成员编号:
ar xN 1 a.a xxx.c.o && mv xxx.c.o xxx.c.1.o
ar xN 2 b.a xxx.c.o && mv xxx.c.o xxx.c.2.o
...
this would be a tedious work, so you will have to write a more sophisticate script to do that job.
这将是一项乏味的工作,因此您将不得不编写更复杂的脚本来完成这项工作。
One optionalsolutions is that you can combine multiple archives into one shared library:
一种可选的解决方案是您可以将多个存档合并到一个共享库中:
g++ -shared -o c.so -Wl,--whole-archive a.a b.a
this way the linker will handle all things for you!
这样链接器将为您处理所有事情!
回答by Guillem Jover
There are at least three ways to do this natively. The first and most portable way is to use libtool. After having built the other libraries also with libtool, you can combine them just by adding the .la libs to an automake libaz_la_LIBADD variable, or directly from a Makefile with something like:
至少有三种方法可以在本机执行此操作。第一种也是最便携的方法是使用 libtool。在使用 libtool 构建其他库之后,您可以通过将 .la 库添加到 automake libaz_la_LIBADD 变量中来组合它们,或者直接从 Makefile 中添加如下内容:
libtool --mode=link cc -static -o libaz.la libabc.la libxyz.la
The other two are at least available when using GNU ar. You can use an MRI script (named for example libaz.mri), such as:
其他两个至少在使用 GNU ar 时可用。您可以使用 MRI 脚本(例如命名为 libaz.mri),例如:
create libaz.a
addlib libabc.a
addlib libxyz.a
save
end
and then execute ar as:
然后执行 ar 为:
ar -M <libaz.mri
Or you can use a thinarchive (option -T
), which will allow adding other archives without getting them nested inside, although the downside is that if you want to distribute the static library, the detached object will be missing:
或者您可以使用精简存档(选项-T
),这将允许添加其他存档而无需将它们嵌套在其中,尽管缺点是如果您想分发静态库,分离的对象将丢失:
ar -rcT libaz.a libabc.a libxyz.a
All the above methods gracefully handle overlapping member names from the original archives.
以上所有方法都可以优雅地处理原始档案中重叠的成员名称。
Otherwise, you'd have to unpack into different directories and repack again, to avoid replacing overlapping member names:
否则,您必须解压缩到不同的目录并重新打包,以避免替换重叠的成员名称:
mkdir abc; cd abc; ar -x ../libabc.a
mkdir xyz; cd xyz; ar -x ../libxyz.a
ar -qc libaz.a abc xyz
回答by Allan Jensen
Even better you perform partial linking on each library and them make an archive of the two resulting object files. That way it operates like shared libraries would
更好的是您在每个库上执行部分链接,它们为两个生成的目标文件制作存档。这样它就像共享库一样运行
You do partial linking with
你做部分链接
gcc -r --nostdlib
so either instead of making the intermediate archive or after reextracting it, run
因此,不要制作中间存档,也不要在重新提取它后运行
gcc -r --nostdlib $CFLAGS $OBJECTS_A -o $LIBNAME_A.o
gcc -r --nostdlib $CFLAGS $OBJECTS_B -o $LIBNAME_B.o
then
然后
ar -cr $LIBNAME_JOINED.a $LIBNAME_A.o $LIBNAME_B.o
回答by law wey
ar -x libx264.a
mkdir sub && cd sub
ar -m ../libx264.a `ar -t ../libx264.a |sort|uniq|grep "\.o"`
ar -x ../libx264.a
now you have two version of "macroblock-10.o"
现在你有两个版本的“macroblock-10.o”
回答by Evgeny Yashin
ar crsT libaz.a libabc.a libxyz.a
Here, you create archive of archives and then 'flatten' (thinning) result with T flag. Not sure how it'll work with same name .o files that might be contained within.
在这里,您创建档案档案,然后使用 T 标志“展平”(细化)结果。不确定它如何处理可能包含在其中的同名 .o 文件。