protobuf android 编译,Android跨平台编译 —— protobuf
前言
正文
直入正題。編譯protobuf的android版本我們使用的環境如下
CMake:?3.6
NDK: r16b
Protobuf:?v3.5.1
OS : Mac os
1? 首先進入到cmake目錄下,創建一個文件夾?build-armeabi-v7a
2?然后我們在根目錄下(protobuf的頂層目錄)添加一個android.sh,代碼如下
cd cmake && cd build-armeabi-v7a
# Step 3. Generate Makefile with CMake.
# -Dprotobuf_BUILD_SHARED_LIBS=ON By default is static library (*.a file). I want a
# shared library (*.so file).
# -DCMAKE_INSTALL_PREFIX Because it's a cross-compiled library. You probably
# want to install the header files and shared library
# in specific folder rather than default /usr/local
# directory.
# -DANDROID_STL=c++_shared For the library using C++11, link to C++11 runtime.
# -DANDROID_LINKER_FLAGS="-landroid -llog" For the library using functions of libandroid.so
# and liblog.so.
# -DANDROID_CPP_FEATURES="rtti exceptions" Most ppl use exception and runtime-type-information
# features in their C++ projects.
#
# Debug Tips:
# Add -LAH to see variables.
#/Users/yxwang/Library/Android/sdk/cmake/3.6.4111459/bin/cmake \
/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=/Users/yxwang/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX= \
-DANDROID_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" \
..
# Step 4. Run Make with generated Makefile.
/cmake --build .
# Step 5 (optional). Install the generated header files and shared library
# to specific folder.
make install
PS:注意這里的android.toolchain.cmake的地址,并不是直接放在sdk/cmake/version/下的那個文件……使用這個文件會報錯。可以在android studio中創建一個擁有c++的項目,然后編譯一次,在.externalNativeBuild/cmake_build_command.txt中能夠找到位置,直接拿來用就行。
另外這里生成的是.a的靜態庫,如果需要.so的動態庫,可以吧上面protobuf_build_shared_libs這個編譯選項設置為on
3. 在運行之前還需要修改原來的 cmake文件
我這里只留下了 protobuf-lite的庫,所以最后也只會生成這個庫。如果需要libprotobuf.so庫,可以把這部分也放出來。
不過libprotoc和protoc是無法使用ndk編譯的,所以一定需要注釋掉。
在項目中使用的時候可能會出現這樣的提示
Error:FAILURE: Build failed with an exception.
* What went wrong:
.....
FAILED: : && /Users/yxwang/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android --gcc-toolchain=/Users/yxwang/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/yxwang/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/yxwang/Library/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions --std=c++1z -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /Users/yxwang/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libTKATPROTOBUF.so -o ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libTKATPROTOBUF.so src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/protobuf-handler.cpp.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/protobuf-message-decorator.cpp.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.dbf.sjs.pb.cc.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.login.pb.cc.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.pb.cc.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.quote.pb.cc.o ../../../../src/main/cpp/protobuf/arm64-v8a/lib/libprotobuf.a -latomic -lm "/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++.a" && :
../../../../src/main/cpp/protobuf/arm64-v8a/lib/libprotobuf.a(common.cc.o): In function `google::protobuf::internal::DefaultLogHandler(google::protobuf::LogLevel, char const*, int, std::__ndk1::basic_string, std::__ndk1::allocator > const&)':
/Users/yxwang/Desktop/protobuf-3.5.1/src/google/protobuf/stubs/common.cc:142: undefined reference to `__android_log_write'
/Users/yxwang/Desktop/protobuf-3.5.1/src/google/protobuf/stubs/common.cc:150: undefined reference to `__android_log_write'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
表示_android_log_write未定義,而是是報在靜態庫中的。
解決方案
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log)
target_link_libraries(TKATPROTOBUF PUBLIC ${PROTOBUF_LIBRARY} ${log-lib})
在使用Protobuf庫的時候,同時添加log-lib庫的鏈接。
總結
以上是生活随笔為你收集整理的protobuf android 编译,Android跨平台编译 —— protobuf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全景视频播放器中OpenGL的相关记录
- 下一篇: matlab代码用python替换_用p