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

bla*_*988 5 cmake protocol-buffers

我有cmake的问题

我在CMakeLists写作

set(PROTOBUF_VERSION"2.4.1")
find_package(Protobuf $ {PROTOBUF_VERSION} EXACT REQUIRED)

但是当我使用protobuf 2.5.0在我的机器上运行cmake时,它成功生成了makefile.
在stdout我只有:

-- 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的版本?

Fra*_*ser 5

我不确定故障是在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()
Run Code Online (Sandbox Code Playgroud)

或选项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()
Run Code Online (Sandbox Code Playgroud)