ubuntu18.04使用ndk编译protobuf库
ndk下载路径:
https://dl.google.com/android/repository/android-ndk-r25c-linux.zip
protobuf下载路径:https://github.com/protocolbuffers/protobuf/releases
下载protobuf源码后,拷贝到ubuntu虚拟机,新建build.sh脚本, 脚本添加如下内容,注意NDK需要修改为解压的对应路径
编译32位库
curpath=$PWD
echo $curpath
mkdir build
chmod +x configure
export PREFIX=$curpath/build
export PATH=/home/work/workspace/app/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
export SYSROOT=/home/work/workspace/app/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/sysroot
export CC="armv7a-linux-androideabi26-clang --sysroot $SYSROOT"
export CXX="armv7a-linux-androideabi26-clang++ --sysroot $SYSROOT"
export CFLAGS="-march=armv7-a -D__ANDROID_API__=26 -fPIC"
export CXXFLAGS="-frtti -fexceptions -march=armv7-a -D__ANDROID_API__=26 -fPIC"
./configure \
--prefix=$PREFIX \
--host=arm-linux-androideabi \
--with-sysroot="${SYSROOT}" \
--enable-static \
--enable-cross-compile \
--with-protoc=protoc \
LIBS="-llog -lz -lc++_static"
make -j 8
make install
编译64位库
export PATH=/home/zj/workspace/app/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
export SYSROOT=/home/zj/workspace/app/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/sysroot
export CC="aarch64-linux-android26-clang --sysroot $SYSROOT"
export CXX="aarch64-linux-android26-clang++ --sysroot $SYSROOT"
export CFLAGS="-D__ANDROID_API__=26 -fPIC"
export CXXFLAGS="-frtti -fexceptions -D__ANDROID_API__=26 -fPIC"
./configure \
--prefix=$PREFIX \
--host=aarch64-linux-android \
--with-sysroot="${SYSROOT}" \
--enable-static \
--enable-cross-compile \
--with-protoc=protoc \
LIBS="-llog -lz -lc++_static"
make -j8
make install
编译产物在build目录下,可以用readelf -h 命令查看库为32或者64位