Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two "ar" static libraries into one?

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.

I want to create a merged static library, not to give both libraries to final link of applications.

like image 976
osgx Avatar asked Sep 29 '10 13:09

osgx


People also ask

What is AR static library?

A static library is a programming concept in which shared libraries with special functionalities, classes or resources are linked to external applications or components, facilitating the creation of stand-alone and executable files.

What is a thin archive?

<thin>. A thin archive only contains a symbol table and references to the file. The file format is essentially a System V format archive where every file is stored without the data sections.

What command is used to create a static library from object files?

ar is used to create static libraries. These are used in software development. And ar is also be used to create package files such as the “.


3 Answers

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 --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:

create libaz.a
addlib libabc.a
addlib libxyz.a
save
end

and then execute ar as:

ar -M <libaz.mri

Or you can use a thin archive (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:

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
like image 146
Guillem Jover Avatar answered Oct 26 '22 09:10

Guillem Jover


You can extract the object from both the .a files and create your .a file using the extracted .os:

ar -x libabc.a
ar -x libxyz.a
ar -c libaz.a  *.o
like image 66
codaddict Avatar answered Oct 26 '22 08:10

codaddict


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:

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.

The only way to extract all members of same name in one archive is to specify the member number by option '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 optional solutions 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!

like image 11
samuel.zzy220 Avatar answered Oct 26 '22 08:10

samuel.zzy220