49

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
  • 12,476
  • 3
  • 53
  • 81
Era
  • 30,460
  • 24
  • 136
  • 197

4 Answers4

86

you can use libtool to do it

libtool -static -o new.a old1.a old2.a
Bruce
  • 7,044
  • 1
  • 24
  • 41
  • 2
    This is actually a better solution than `ar`. Thanks! – Era 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
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
  • 4,723
  • 48
  • 46
0

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,479
  • 11
  • 101
  • 120
-1

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

justin
  • 103,167
  • 13
  • 178
  • 224