我正在使用CMake作为构建工具,并为我在项目中使用的所有库预先打包了二进制文件。其中一个库是Protobuf,通过Conan IO下载。所以,我想使用柯南下载的Protobuf,而不是Linux已经安装的Protobuf。问题是我在运行CMake
时出现以下错误:
CMake Warning at /home/username/Documents/project/test/build/venv/lib/python3.6/site-packages/cmake/data/share/cmake-3.10/Modules/FindProtobuf.cmake:455 (message):
Protobuf compiler version doesn't match library version 3.6.1
Call Stack (most recent call first):
/home/username/Documents/project/test/script/cmake/Env.cmake:139 (include)
CMakeLists.txt:6 (include)
-- Found Protobuf: /home/username/Documents/project/test/build/venv/.conan/data/Protobuf/3.6.1/project/dev/package/80043e232e8ab07f4b25e67652a9490d9ad33d91/lib/libprotobuf.so;-lpthread (found version "3.6.1")
CMake Warning at /home/username/Documents/project/test/build/venv/lib/python3.6/site-packages/cmake/data/share/cmake-3.10/Modules/FindProtobuf.cmake:455 (message):
Protobuf compiler version doesn't match library version 3.6.1
Call Stack (most recent call first):
/home/username/Documents/project/test/src/shared/bysp/CMakeLists.txt:9 (find_package)
有没有办法解决这个问题?这会导致错误吗?
我在树莓中添加下一个选项到 CMake 调用中解决了它。
-D Protobuf_PROTOC_EXECUTABLE=/usr/bin/protoc
就我而言,问题是cmake
实际上找不到我的Protobuf编译器的正确路径。
如果您检查 CMake 的 FindProtobuf.cmake 模块 (/usr/share/cmake-3.13/Modules/FindProtobuf.cmake),则会出现警告消息:
if(NOT "${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" VERSION_EQUAL "${Protobuf_VERSION}")
message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}"
" doesn't match library version ${Protobuf_VERSION}")
endif()
预计警告消息将打印出系统中安装的实际 Protobuf 编译器版本。但是,实际版本号不是消息的一部分。
在FindProtobuf.cmake模块中,如果在尝试获取 Profobuf 编译器的路径时检查上面的一些行,它会:
# Find the protoc Executable
find_program(Protobuf_PROTOC_EXECUTABLE
NAMES protoc
DOC "The Google Protocol Buffers Compiler"
PATHS
${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
)
以后没有代码可以检查是否可以找到路径。下一步是执行程序并检查版本:
# Check Protobuf compiler version to be aligned with libraries version
execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --version
OUTPUT_VARIABLE _PROTOBUF_PROTOC_EXECUTABLE_VERSION)
if("${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" MATCHES "libprotoc ([0-9.]+)")
set(_PROTOBUF_PROTOC_EXECUTABLE_VERSION "${CMAKE_MATCH_1}")
endif()
而且,至少是我的系统,没有protoc
程序(但protoc-c
),此检查失败,这也是警告消息不打印系统安装的Protobuf编译器实际版本的原因。
解决方案是将find_program
语句更改为以下内容:
# Find the protoc Executable
find_program(Protobuf_PROTOC_EXECUTABLE
NAMES protoc-c protoc
DOC "The Google Protocol Buffers Compiler"
PATHS
${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Release
${Protobuf_SRC_ROOT_FOLDER}/vsprojects/${_PROTOBUF_ARCH_DIR}Debug
)
并在重新运行cmake
之前删除CMakeCache.txt和CMakeFiles/。
不要忘记安装编译器,在 Ubuntu 和 Debian 上,您应该安装库和编译器,如下所示C++:
sudo apt install libprotobuf-dev protobuf-compiler
看起来您下载的协议由于某种原因无法启动。尝试获取protobuf版本
./protoc --version
在相应的目录中。
基于arhuaco的答案,它可以在cmake配置中使用:
set(Protobuf_PROTOC_EXECUTABLE /usr/bin/protoc)