gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:07:54 26 4
gpt4 key购买 nike

我有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com