protobuf DNK下的编译
protobuffer 编译配置
mkdir -p ./cmake/build
cd ./cmake/build
cmake \ -Dprotobuf_BUILD_SHARED_LIBS=OFF \ -Dprotobuf_BUILD_STATIC_LIBS=ON \ -DCMAKE_VERBOSE_MAKEFILE=ON \ -Dprotobuf_BUILD_TEST=OFF \ -Dprotobuf_BUILD_EXAMPLES=OFF \ -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake \ -DCMAKE_BUILD_TYPE=Release \ -DANDROID_NDK=/home/floyd/ndk \ -DANDROID_TOOLCHAIN=clang \ -DANDROID_ABI=armeabi-v7a \ -DANDROID_NATIVE_API_LEVEL=16 \ -DANDROID_STL=c++_shared \ -DANDROID_LINKER_FLAGS="-landroid -llog" \ -DANDROID_CPP_FEATURES="rtti exceptions" \ ..
如果编译出现错误 “undefined reference to `__android_log_write'”,编辑代码手动选择DefaultLogHandler的实现
./toolchain.cmake内容
SET(CMAKE_SYSTEM_NAME Linux) SET(CMAKE_SYSTEM_VERSION 1) set(CMAKE_C_COMPILER arm-linux-androideabi-gcc) #set(CMAKE_FIND_ROOT_PATH /root/NanoPi2/linux-3.4.y/usr/include) SET(CMAKE_FIND_ROOT_PATH ${SDK_SYS_ROOT_PATH}) SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
protobuf-c编译
将libprotobuf.a libprotobuf-lite.a libprotobuf-c.a拷贝到工具链的目录/home/floyd/ndk/lib
protobuf-c编译配置
./configure CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ --host=arm-linux protobuf_LIBS="-L//home/floyd/ndk/lib -lprotobuf"
检查Makefile
protoc_c_protoc_gen_c_LDADD = \
$(protobuf_LIBS) \
-lprotoc
更改为(依赖顺序protoc protobuf_LIBS):
protoc_c_protoc_gen_c_LDADD = \ -lprotoc \ $(protobuf_LIBS)
make
基于protobufc的应用程序
只需要连接“-lprotobuf-lite -lprotobuf-c”