构建iOS静态库及动态库

前言

通常,需要配置host,以及所使用的SDK所在的目录、C编译器路径、C++编译器路径。

host通常使用--host选项进行指定。

SDK通常使用--with-sysroot选项或--sysroot选项进行指定。

C编译器可使用xcrun -f clang取得。

C++编译器可使用xcrun -f clang++取得。

--prefix选项指定构建输出目录。

--disable-shared--enable-static选项指定生成静态库。

--enable-shared--disable-static选项指定生成动态库。

具体选项可在各第三方库根目录下执行./configure --help进行查看。

host

  • 对于arm64架构,hostaarch64-apple-darwin
  • 对于armv6armv7armv7s架构,hostarm-apple-darwin
  • 对于i386架构,hosti386-apple-darwin
  • 对于x86_64架构,hostx86_64-apple-darwin

SDK

  • 对于arm架构,可使用xcrun --show-sdk-path --sdk iphoneos取得
  • 对于i386x86_64架构,可使用xcrun --show-sdk-path --sdk iphonesimulator取得

环境变量

在编译时,也可能需要修改如下环境变量,如:

1
2
3
4
5
6
export CC="$CLANG -arch $a -isysroot $SDK"
export CXX="$CLANG_XX -arch $a -isysroot $SDK"
export CXXFLAGS="-arch $a -isysroot $SDK"
export CFLAGS="-arch $a -isysroot $SDK"
export LDFLAGS="-isysroot $SDK"
export LIBS="-L$SDK/usr/lib"

其中,

1
2
3
CLANG="`xcrun -f clang`"
CLANG_XX="`xcrun -f clang++`"
SDK="`xcrun --show-sdk-path --sdk iphoneXXX`"

将多个只支持单个CPU架构的库文件合并为通用库文件

1
lipo lib-armv7s.a lib-arm64.a -create -output lib-fat.a
1
lipo lib-armv7s.dylib lib-arm64.dylib -create -output lib-fat.dylib

构建iOS静态库及动态库
https://daniate.github.io/2018/04/15/构建iOS静态库及动态库/
作者
Daniate
发布于
2018年4月15日
许可协议