51

I tried the approach in this question, but it seems the linux version of ar is not the same as the mac version since I failed to combine the object files again.

What I basically want to do is is merge another static library into my Xcode static library build product via a run-script build phase.

Unfortunately I can't compile the other library directly into my project because it has it's own build system (therefore I use the compiled libs).

I think it should be possible to merge the other library via ar into the Xcode generated library without decompiling the build product. How do I accomplish this?

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91

4 Answers4

88

you can use libtool to do it

libtool -static -o new.a old1.a old2.a
Bruce
  • 7,094
  • 1
  • 25
  • 42
  • 2
    This is actually a better solution than `ar`. Thanks! –  Nov 17 '11 at 18:33
  • any command line (bash), you should have libtool in PATH provided you have the developpers' extension. Alternatively, as a post build step in xcode. – Bruce Jan 23 '13 at 16:09
  • 1
    You need to download and install `libtool`. This is in the Xcode's command line tool. – sunkehappy Mar 15 '13 at 09:37
  • @MohamMad I've added this call as a build phase script. More details at http://stackoverflow.com/a/21225126/239408 – xverges Jan 20 '14 at 02:04
  • This was exactly what I needed. Added it as a run-script build step in XCode and worked like a charm. Thanks. – Ternary Aug 11 '14 at 14:48
  • Does this command rename object files with overlapping names so that duplicate files can both be accessed in the resulting archive library? On MacOS the lib tool command complains about duplicate names. – Chris Quenelle Aug 04 '22 at 23:33
8

If you're dealing with multi-architecture static libraries, a bit of extra manipulation is required to thin each library, combine the thinned versions, and then fatten the result. Here's a handy script which you can edit to your satisfaction which does all that in one. The example takes three iOS libraries path/to/source/libs/libone.a, path/to/source/libs/libtwo.a, and path/to/source/libs/libthree.a and merges them into a single library libcombined.a.

#! /bin/bash

INPATH="path/to/source/libs"

LIBPREFIX="lib"
LIBS="one two three"
LIBEXT=".a"

OUT="combined"

ARCHS="armv7 armv7s arm64"

for arch in $ARCHS
do
  for lib in $LIBS
  do
    lipo -extract $arch $INPATH/$LIBPREFIX$lib$LIBEXT -o $LIBPREFIX$lib-$arch$LIBEXT
  done
  INLIBS=`eval echo $LIBPREFIX\{${LIBS// /,}\}-$arch$LIBEXT`
  libtool -static -o $LIBPREFIX$OUT-$arch$LIBEXT $INLIBS
  rm $INLIBS
done

OUTLIBS=`eval echo $LIBPREFIX$OUT-\{${ARCHS// /,}\}$LIBEXT`
lipo -create $OUTLIBS -o $LIBPREFIX$OUT$LIBEXT
rm $OUTLIBS
bleater
  • 5,098
  • 50
  • 48
  • This script did not seem to deal with overlapping names. The last limo command warned about duplicate names and the TOC did not show any for of disambiguation, so I think it would not work. – Chris Quenelle Aug 04 '22 at 23:32
  • This doesn't seem to be necessary any more, `libtool -static` can directly combine fat static libraries – Alan Birtles May 18 '23 at 16:59
-1

You should just be able to link one to the other. So... just use ld to merge the images.

justin
  • 104,054
  • 14
  • 179
  • 226
-1

You should use ar -r to create an archive on MacOS:

ar -x libabc.a
ar -x libxyz.a
ar -r libaz.a  *.o
sergio
  • 68,819
  • 11
  • 102
  • 123