126

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.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
osgx
  • 90,338
  • 53
  • 357
  • 513
  • 10
    See also: [`libtool`-based solution](http://stackoverflow.com/a/8170851/196561): `libtool -static -o new.a old1.a old2.a` – osgx Mar 18 '14 at 14:56
  • 2
    it works perfect, just a little doubt if those libraries have common file.o(but functionality wise they are different) will it still work? – bindingofisaac Jul 07 '15 at 13:40
  • libtool -static -o new.a old1.a old2.a dosen't work on linux(centos 7) – vacing Oct 09 '20 at 09:30

9 Answers9

153

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
Guillem Jover
  • 2,090
  • 2
  • 11
  • 16
  • 27
    For those that want a normal archive (not thin), one simple thing that can be done is create a thin archive, then convert it to a normal archive. Something like: `ar cqT libaz.a libabc.a libxyz.a && echo -e 'create libaz.a\naddlib libaz.a\nsave\nend' | ar -M`. This creates a temporary thin `libaz.a`, and then converts the thin archive into a normal one (so you can move/distribute it). This also gracefully handles when your library names have special characters (spaced, pluses, or commas) (i.e. `ar cqT libbundle.a libfoo++.a 'libbar baz.a'`). But +1 from me! – Cornstalks May 28 '14 at 22:58
  • 1
    What is the downside to the first MRI script example given? – j b Nov 13 '14 at 13:06
  • Nice answer! Good to see some options that don't require you extracting and re-achiving. Also i think @Cornstalks idea is good. Maybe should be added to the answer? – Lightbulb1 Apr 02 '15 at 14:12
  • 1
    Hey when I try to use the command for `libtool` I get these erros: `libtool: link: unable to infer tagged configuration libtool: error: specify a tag with '--tag' `Any ideas how to fix that ? – Lars Nielsen Jan 11 '19 at 09:54
  • @Guillem @Cornstalks Great answer. What if the `--Wl,-whole-archive` option is required in original linking command for multiple lib*.a, and I need to combine all lib*.a into `one.a`. When linking again, `--Wl,-whole-archive` won't work with `one.a`. What is your suggestion? https://stackoverflow.com/questions/56323197/dilemma-in-linux-combined-static-library-by-ar-command-with-wl-whole-archive – thinkdeep May 28 '19 at 02:28
  • It's a pity they don't even mention MRI mode in some newer editions of the AR man pages. The feature is exceptionally useful in the complex builds (because the script itself can be a dependency), while "thin" archive approach (the supposedly more modern alternative to merging) has a nasty habit of breaking CI build pipelines in most unexpected ways. – oakad Jun 06 '19 at 03:15
  • if there are overlapping names in an archive, the last method of extracting file will fail, because .o with the same name will overlap – vacing Oct 09 '20 at 08:45
  • The last approach seems to use folders as input for ar -qc but when I do I get an error : File format not recognized – BenjaminB Oct 26 '21 at 09:43
  • just as a note the `--mode=link cc` part is not compatible with apples libtool as of XCode 13. The equivalent there seems to be instead `libtool --mode=link cc -static -o libaz.la libabc.la libxyz.la` to do `clang -r -o libaz.o --all_load libabc.la libxyz.la; ar cr libaz.la libaz.o` – PeterT Jun 23 '22 at 09:19
69

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
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 75
    Danger, Will Robinson! This works _only_ if the names of the members in libabc.a and libxyz.a don't overlap. Otherwise you'll overwrite one and it'll be lost. – David Given Jun 07 '13 at 15:45
  • 7
    Moreover, `libabc.a` may contain objects having the same name (originated form different directories) -- re-assembling won't work then! – Igor R. May 25 '14 at 08:18
  • 18
    `ar -c` didn't work for me (Ubuntu 14.04). I got `ar: no operation specified`. I did `ar -qc` instead and that worked well. – Max Dec 31 '14 at 15:02
  • ar t lib.a can be used for view the files in library without actually extracting the files. – raj_gt1 May 11 '15 at 14:56
  • how can I do that in automake ? – KRoy May 29 '16 at 20:34
12

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!

osgx
  • 90,338
  • 53
  • 357
  • 513
samuel.zzy220
  • 115
  • 1
  • 3
2

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
osgx
  • 90,338
  • 53
  • 357
  • 513
  • It's not really answering question asked - as he asked for libraries. Many times you don't even have sources for libraries given, or want to keep them prebuilt from other reasons. – pholat May 05 '19 at 07:01
  • It does answer it, you can do with libraries as well, though for static libs you need -Wl,--whole-archive. – Allan Jensen Jul 16 '21 at 11:01
2

You can always do partial linking:

gcc -r -o libnew.o -Wl,--whole-archive libabc.a libxyz.a
ar cr libnew.a libnew.o
Allan Jensen
  • 518
  • 4
  • 8
1

This question is specific to Linux. If you're looking for how to do this on macOS, the answer is libtool, as explained here:

libtool -static -o new.a old1.a old2.a
tbodt
  • 16,609
  • 6
  • 58
  • 83
1

Here is a bash script to link all .a libs from a specified directory into a single library.

Usage: ./unify-static-libs.sh myFinalLib.a /path/to/input/libs

#!/bin/bash
#
# Creates one static library from several.
#
# Usage: copy all your libs to a single directory and call this script.
#
if [[ $# -ne 2 ]]; then
  echo "Usage: unify-static-libs.sh output-name path-to-libs"
  exit 2
fi
# Inputs
LIBNAME=$1
LIBSDIR=$2
# Tmp dir
OBJDIR=/tmp/unify-static-libs
mkdir -p ${OBJDIR}
# Extract .o
echo "Extracting objects to ${OBJDIR}..."
for i in ${LIBSDIR}/*.a
do
    echo $i
    ar --output $OBJDIR -x $i
done
# Link objects into a single lib
echo "Creating $LIBNAME from objects..."
ar -crs $LIBNAME $OBJDIR/*.o
# Clean up
rm -rf ${OBJDIR}
echo "Done."

Danijel
  • 8,198
  • 18
  • 69
  • 133
0
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"

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