如何实现protobuf在XCODE上armv7/armv7s/i386的交叉编译
发布网友
发布时间:2022-04-24 22:48
我来回答
共1个回答
热心网友
时间:2022-04-10 08:37
步骤一:部署protoc.exe
1)sudo su ---进入管理模式
//以下操作切换至protobuf文件夹下
2)./configure
3)make
4)make check
5)make install
此时可以查看protobuf文件夹下面的makefile文件,可以查看-build表示的本机环境以及-host表示的编译库的运行环境
我本地的-build=x86_64-apple-darwin12.3.0
-host=x86_64-apple-darwin12.3.0
(这两个参数在后续配置脚本需要用到,与后面脚步的i686-apple-darwin12.3.0以及arm-apple-darwin12.3.0后缀“arm-apple-darwin12.3.0”保持一致)
6)make distclean清理已生成的makefile,为交叉编译配置新makefile做准备
步骤二:配置交叉编译
1)执行脚本ios-build.sh,脚本内容如下:
configure_for_platform() {
export PLATFORM=$1
#export PLATFORM=iPhoneOS
echo "Platform is ${PLATFORM}"
if [ "$PLATFORM" == "iPhoneSimulator" ]; then
export ARCHITECTURE=i386
export ARCH=i686-apple-darwin12.3.0
fi
if [ "$PLATFORM" == "iPhoneOS" ]; then
export ARCHITECTURE=$2
export ARCH=arm-apple-darwin12.3.0
fi
export ARCH_PREFIX=$ARCH-
export SDKVER="6.1"
#sdk版本号必须正确
export
DEVROOT=/Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer
export SDKROOT="$DEVROOT/SDKs/${PLATFORM}$SDKVER.sdk"
export
PKG_CONFIG_PATH="$SDKROOT/usr/lib/pkgconfig:$DEVROOT/usr/lib/pkgconfig"
export AS="$DEVROOT/usr/bin/as"
export ASCPP="$DEVROOT/usr/bin/as"
export AR="$DEVROOT/usr/bin/ar"
export RANLIB="$DEVROOT/usr/bin/ranlib"
#export CPP="$DEVROOT/usr/bin/c++"
#export CXXCPP="$DEVROOT/usr/bin/c++"
export CC="$DEVROOT/usr/bin/gcc"
export CXX="$DEVROOT/usr/bin/g++"
export LD="$DEVROOT/usr/bin/ld"
export STRIP="$DEVROOT/usr/bin/strip"
export LIBRARY_PATH="$SDKROOT/usr/lib"
export CPPFLAGS=""
#export CFLAGS="-arch armv7 -fmessage-length=0 -pipe -fpascal-strings
-miphoneos-version-min=4.0 -isysroot=$SDKROOT -I$SDKROOT/usr/include
-I$SDKROOT/usr/include/c++/4.2.1/"
export CFLAGS="-arch ${ARCHITECTURE} -fmessage-length=0 -pipe
-fpascal-strings -miphoneos-version-min=4.0 -isysroot=$SDKROOT
-I$SDKROOT/usr/include -I$SDKROOT/usr/include/c++/4.2.1/"
export CXXFLAGS="$CFLAGS"
#export LDFLAGS="-isysroot='$SDKROOT' -L$SDKROOT/usr/lib/system
-L$SDKROOT/usr/lib/"
export LDFLAGS="-arch ${ARCHITECTURE} -isysroot='$SDKROOT'
-L$SDKROOT/usr/lib/system -L$SDKROOT/usr/lib/"
./configure --host=${ARCH} --with-protoc=protoc --enable-static
--disable-shared
}
mkdir ios-build
#build for iPhoneSimulator
configure_for_platform iPhoneSimulator
make clean
make
cp src/.libs/libprotobuf-lite.a ios-build/libprotobuf-lite-i386.a
#提取完整版本(i386)
cp src/.libs/libprotobuf.a ios-build/libprotobuf-i386.a
#build for iPhoneOS armv7
configure_for_platform iPhoneOS armv7
make clean
make
cp src/.libs/libprotobuf-lite.a ios-build/libprotobuf-lite-armv7.a
#提取完整版本(armv7)
cp src/.libs/libprotobuf.a ios-build/libprotobuf-armv7.a
#build for iPhoneOS armv7s
configure_for_platform iPhoneOS armv7s
make clean
make
cp src/.libs/libprotobuf-lite.a ios-build/libprotobuf-lite-armv7s.a
#提取完整版本(armv7s)
cp src/.libs/libprotobuf.a ios-build/libprotobuf-armv7s.a
make clean
#cerate a fat library containing all achitectures in libprotobuf-lite.a
xcrun -sdk iphoneos lipo -arch armv7 ios-build/libprotobuf-lite-armv7.a -arch
armv7s ios-build/libprotobuf-lite-armv7s.a -arch i386
ios-build/libprotobuf-lite-i386.a -create -output
ios-build/libprotobuf-lite.a
#合并三个完整版本(libprotobuf.a)
xcrun -sdk iphoneos lipo -arch armv7 ios-build/libprotobuf-armv7.a -arch
armv7s ios-build/libprotobuf-armv7s.a -arch i386 ios-build/libprotobuf-i386.a
-create -output ios-build/libprotobuf.a
2)将打包生成的libprotobuf-lite.a和libprotobuf.a复制至工程下进行编译,可以编译protobuf在xcode上的模拟器版本和真机版本,完成交叉编译。