博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android跨平台编译 —— protobuf
阅读量:6856 次
发布时间:2019-06-26

本文共 5786 字,大约阅读时间需要 19 分钟。

hot3.png

前言

    前言都在 

正文

    直入正题。编译protobuf的android版本我们使用的环境如下

                CMake: 3.6

                NDK: r16b
                Protobuf: v3.5.1
                OS : Mac os 

    首先我们下载protobuf的代码 直接下载cpp平台的就可以了。

    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文件

    191836_AKYT_2398062.png

    191913_rX8y_2398062.png

    我这里只留下了 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.orgBUILD 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库的链接。

转载于:https://my.oschina.net/zzxzzg/blog/1621684

你可能感兴趣的文章
IOS KVC运用代码详解
查看>>
java enum转换工具类
查看>>
Visual Studio 文件没发布出来
查看>>
linux 小脚本
查看>>
不要为数据持久层编写单元测试
查看>>
使用Mycat实现MySQL数据库的读写分离
查看>>
linux安装软件命令
查看>>
在centos 6 下安装mysql 5.6
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Linux集群服务知识点总结及通过案例介绍如何实现高性能web服务
查看>>
linux 运维从初级到高级的修炼
查看>>
关于Hadoop系列文章
查看>>
JAVA学习日志(7-3-抽象类)
查看>>
Linux date命令的用法(转)
查看>>
Linux shell 之 提取文件名和目录名的一些方法
查看>>
test.
查看>>
树莓派3B连接WIFI
查看>>
springMVC启动过程源码解析(一)
查看>>
linux中生成考核用的FAT32文件系统结构样例(一)
查看>>