【问题标题】:Use protobuf in Android NDK using CMake使用 CMake 在 Android NDK 中使用 protobuf
【发布时间】:2021-07-09 15:37:12
【问题描述】:

我需要在android ndk的c++代码中使用protobuf库。换句话说,让我自动生成的something.pb.h 在它想要包含#include <google/protobuf/port_def.inc> 之类的文件时感到高兴。

我确实看到了一些方法,但它们都使用旧的Android.mk 方法,而我需要使用新的CMakeLists.txt 方法(我的项目中完全没有Android.mk 这样的东西)。

因此,我该怎么办?感谢您的任何建议!


我尝试过的:

首先brew install protobuf。然后,在 CMakelists.txt

SET(PROTOBUF_INCLUDE_DIR "/usr/local/include/")
# have also tried this: find_package(Protobuf REQUIRED)
# have also tried this: find_package(protobuf CONFIG REQUIRED)
find_package(protobuf REQUIRED)
message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")

target_link_libraries(vision_utils
        ${PROTOBUF_LIBRARIES}
        # have also tried this: protobuf::libprotobuf
)

但上述不同尝试的各种错误:

CMake Error at CMakeLists.txt:25 (find_package):
Could not find a package configuration file provided by "protobuf" with any
of the following names:

CMake Error at CMakeLists.txt:107 (add_library):
Target "vision_utils" links to target "protobuf::libprotobuf" but the
target was not found.  Perhaps a find_package() call is missing for an
IMPORTED target, or an ALIAS target is missing?

【问题讨论】:

  • 似乎formula for protobuf 使用./configure 方式构建项目。这样就不会创建 find_package(Protobuf) 工作所需的 protobuf-config.cmake 文件。使用 CMake 构建 protobuf 时会创建所需的文件:github.com/protocolbuffers/protobuf/tree/master/cmake.
  • @Tsyvarev 谢谢!我对cmake不是很熟悉,请您多解释一下吗?
  • @Tsyvarev 另外我在 macos 上,但你提到的 README 是针对 windows 的。
  • 是的,README 提到了 Windows,但它也应该可以在其他平台上运行。实际上,CMake 本身提供了脚本FindProtobuf.cmake,所以它可以用于find_package(Protobuf),而不是脚本protobuf-config.cmake。正确的调用是 find_package(Protobuf REQUIRED),如果你的 CMake 版本是 3.9 或更高版本,那么这个调用应该创建 IMPORTED 目标 protobuf::libprotobuf。 (对于较低的 CMake 版本,请使用变量 Protobuf_INCLUDE_DIRSProtobuf_LIBRARIES)。
  • 我试过但是不行,如问题所示:(

标签: android c++ cmake android-ndk protocol-buffers


【解决方案1】:

尝试使用手动生成 protobuf 构建文件

cmake -G "MinGW Makefiles" \
 -DCMAKE_TOOLCHAIN_FILE={NDK PATH}/build/cmake/android.toolchain.cmake \
 -DCMAKE_MAKE_PROGRAM={NDK PATH}/prebuilt/{YOUR PLATFORM}/bin/make \
 -Dprotobuf_BUILD_TESTS=OFF \
  {PROTOBUF SOURCE PATH}/cmake

构建

cmake --build . --target libprotobuf

然后在CMakeLists.txt设置

set(Protobuf_INCLUDE_DIR {{PROTOBUF SOURCE PATH}/src}) 
set(Protobuf_LIBRARIES {build files path})

here得到这个解决方案

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 2018-11-04
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多