1. 下载NDK工具,具体步骤略
2. 配置~/.profile文件,添加环境变量如下:
# set PATH so it includes user's private bin directories
export NDK=$HOME/code/android-ndk-r22b
#export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# Only choose one of these, depending on your device...
# 编译不同平台的so,需要打开不同平台的开关
#export TARGET=aarch64-linux-android
export TARGET=armv7a-linux-androideabi
#export TARGET=i686-linux-android
#export TARGET=x86_64-linux-android
# Set this to your minSdkVersion.
export API=21
# Configure and build.
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip
3. 下载protobuf源码,进入主目录:
cd protobuf目录
make clean
./configure --host $TARGET --with-protoc=protoc --enable-shared --enable-cross-compile
make
4. C++源码目录为:protobuf目录/src,生成的so在:protobuf目录/src/.libs目录下