【发布时间】: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_DIRS和Protobuf_LIBRARIES)。 -
我试过但是不行,如问题所示:(
标签: android c++ cmake android-ndk protocol-buffers