1. 利用lipo命令来合并模拟器和真机库:
用模拟器编译生成的静态库是i386架构的,而ios是arm6/arm7/arm7s架构的,架构不兼容,所以用模拟器编译的静态库在真机上不能用。
要使静态库编译出来,模拟器和真机都可以使用的,可以分别编译生成libxxx.a,然后用lipo来合并这两个libxxx.a
如下:
打开终端,cd到build目录,然后使用lipo命令如下:
lipo -create Release-iphoneos/libxxx.a Release-iphonesimulator/libxxx.a -output libxxx.a
将合并生成一个通用的libxxx.a
lipo -info命令用来查看静态库的类型
lipo -info Release-iphoneos/libxxx.a
lipo -info Release-iphonesimulator/libxxx.a
2. 利用脚本直接生成通用的静态库:
创建Aggregate:
在静态库工程里新建一个target(File/New/Target), 在弹出的框中在左边选择iOS下面的Other, 然后在右边选择Aggregate:
创建好Aggregate之后,在Target下面选择刚刚创建的Aggredate, 然后选择"Editor/Add Build Phase/Add Run Script Build Phase"
然后在Run Script下面添加以下脚本:
# define output folder environment variable
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# Step 1. Build Device and Simulator versions
xcodebuild -target Demo ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
xcodebuild -target Demo -configuration ${CONFIGURATION} -sdk iphonesimulator -arch i386 BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 2. Create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a"
# Last touch. copy the header files. Just for convenience
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include" "${UNIVERSAL_OUTPUTFOLDER}/"
最后编译,编译成功会在Build目录下面出现Debug-universal目录,该目录里面生成的libxxx.a就是合并的静态库。