cmake 忽略 findPackage for protobuf 中的确切选项

标签 cmake protocol-buffers

我有 cmake 的问题

我在写 CMakeLists

设置(PROTOBUF_VERSION“2.4.1”)
find_package(Protobuf ${PROTOBUF_VERSION} 完全需要)

但是当我使用 protobuf 2.5.0 在我的机器上运行 cmake 时,它​​会成功生成 makefile。
在标准输出中我只有:
-- Found PROTOBUF: /usr/local/lib/libprotobuf.so
但是对于 ZLIB,我有
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.7")
可能,protobuf 在库中不包含它自己的版本。
有没有办法指定protobufer的版本?

最佳答案

我不确定故障是在 protobuf 中还是在 CMake 模块中,但您在这里有几个选择。

如果 find_package调用成功,您应该可以访问 protobuf 包含路径和 protoc 编译器。您可以阅读 ${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h 的内容并进行正则表达式搜索 #define GOOGLE_PROTOBUF_VERSION ,或者您可以调用 protoc --version并在输出中搜索正确的版本。

因此,对于选项 1,您可以执行以下操作:

find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED)
if(NOT EXISTS "${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h")
  message(FATAL_ERROR "Failed to find protobuf headers")
endif()

file(STRINGS "${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h" Found
     REGEX "#define GOOGLE_PROTOBUF_VERSION 2004001")
if(NOT Found)
  message(FATAL_ERROR "Didn't find v2.4.1 of protobuf")
endif()

或对于选项 2:

find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED)
if(NOT PROTOBUF_PROTOC_EXECUTABLE)
  message(FATAL_ERROR "Failed to find protoc")
endif()

execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --version
                OUTPUT_VARIABLE VersionInfo)
string(FIND "${VersionInfo}" "2.4.1" Found)
if(Found LESS 0)
  message(FATAL_ERROR "Didn't find v2.4.1 of protobuf")
endif()

关于cmake 忽略 findPackage for protobuf 中的确切选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920464/

相关文章:

serialization - 支持和类型的高性能对象序列化库

c - 为 Win64 构建 mongo-c-driver-1.16.2 时出现问题

python - 属性错误 : Assignment not allowed to composite field "task" in protocol message object

c++ - CMake 找不到源文件

installation - install(TARGETS…)和add_subdirectory

python - 为什么protobuf的内存比python中的普通dict+list小?

c# - 如何通过 Protobuf/Protobuf-net 使用某些对象继承的列表/数组?

C++ 网络序列化

variables - 我可以将 CMake 变量设置为 bool 表达式的结果吗?

c++ - CMake生成器表达式,区分C/C++代码