实际开发过程中,遇到编译iOS库时,用到的第三方静态库不是iOS平台的,因此需要自己编译静态库使用。下载第三库的源码,然后解压,之后把下面的脚本文件放在源码解压后的目录,然后执行这个脚本(这个脚本是在网上查找到的,做的很不错)。最后生成会生成fat-libtool
和thin-libtool
目录,fat-libtool
下面是所有iOS使用的平台合成一个静态库,我使用的是这个目录下静态库。如果不需要每个平台都集成,那么可以选择thin-libtool
下面相应平台的静态库就可以了。
#!/bin/sh
make distclean
CONFIGURE_FLAGS="--disable-shared --disable-frontend"
ARCHS="arm64 armv7s x86_64 i386 armv7"
# directories
# SOURCE是下载的第三方库源码包,解压后的目录,可以把sh脚本放到这个目录,source改为""
SOURCE=""
# FAT是所有指令集build后,输出的目录,所有静态库被合并成一个静态库
FAT="fat-libtool"
# SCRATCH是下载源码包,解压后的目录
SCRATCH="./"
# must be an absolute path
# THIN 各自指令集build后输出的静态库所在的目录,每个指令集为一个静态库
THIN=`pwd`/"thin-libtool"
COMPILE="y"
LIPO="y"
if [ "$*" ]
then
if [ "$*" = "lipo" ]
then
# skip compile
COMPILE=
else
ARCHS="$*"
if [ $# -eq 1 ]
then
# skip lipo
LIPO=
fi
fi
fi
if [ "$COMPILE" ]
then
CWD=`pwd`
echo "$CWD/$SOURCE........."
for ARCH in $ARCHS
do
echo "building $ARCH..."
mkdir -p "$SCRATCH/$ARCH"
cd "$SCRATCH/$ARCH"
if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
then
PLATFORM="iPhoneSimulator"
if [ "$ARCH" = "x86_64" ]
then
SIMULATOR="-mios-simulator-version-min=7.0"
HOST=x86_64-apple-darwin
else
SIMULATOR="-mios-simulator-version-min=5.0"
HOST=i386-apple-darwin
fi
else
PLATFORM="iPhoneOS"
SIMULATOR=
HOST=arm-apple-darwin
fi
XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang -arch $ARCH"
#AS="$CWD/$SOURCE/extras/gas-preprocessor.pl $CC"
CFLAGS="-arch $ARCH $SIMULATOR"
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"
CC=$CC $CWD/$SOURCE/configure \
$CONFIGURE_FLAGS \
--host=$HOST \
--prefix="$THIN/$ARCH" \
CC="$CC" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"
make -j3 install
cd $CWD
done
fi
if [ "$LIPO" ]
then
echo "building fat binaries..."
mkdir -p $FAT/lib
set - $ARCHS
CWD=`pwd`
cd $THIN/$1/lib
for LIB in *.a
do
cd $CWD
lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
done
cd $CWD
cp -rf $THIN/$1/include $FAT
fi