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?

4 Answers 4

90

you can use libtool to do it

libtool -static -o new.a old1.a old2.a 
Sign up to request clarification or add additional context in comments.

6 Comments

This is actually a better solution than ar. Thanks!
any command line (bash), you should have libtool in PATH provided you have the developpers' extension. Alternatively, as a post build step in xcode.
You need to download and install libtool. This is in the Xcode's command line tool.
@MohamMad I've added this call as a build phase script. More details at stackoverflow.com/a/21225126/239408
This was exactly what I needed. Added it as a run-script build step in XCode and worked like a charm. Thanks.
|
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 

2 Comments

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.
This doesn't seem to be necessary any more, libtool -static can directly combine fat static libraries
-1

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

Comments

-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 

1 Comment

This doesn't work at all. You're going to overwrite files that have the same .o names.