使用CMake查找Protobuf包

问题描述 投票:0回答:2

我似乎无法让 CMake 始终找到我的 protobuf 包。我想包含 protobuf 源并将其构建为我的项目的一部分。我正在使用 protobuf 3.15.8protobuf-c 1.3.3 (作为控制示例很有用)。

这是示例的目录设置:

- CMakeLists.txt
- deps/
       - CMakeLists.txt
       - protobuf/            # The protobuf 3.15.8 repository
       - protobuf-c/          # The protobuf-c 1.3.3 repository
- target/
         - CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(PROTOBUF_IMPORT_FAIL)
add_subdirectory(deps)
add_subdirectory(target)

deps/CMakeLists.txt

add_subdirectory(protobuf/cmake)
set(Protobuf_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake/lib")
set(Protobuf_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/protobuf/src")
set(Protobuf_PROTOC_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake/libprotoc.a")
add_subdirectory(protobuf-c/build-cmake)

target/CMakeLists.txt

set(Protobuf_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/../deps/protobuf/cmake/lib")
set(Protobuf_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}../deps/protobuf/src")
message("This is the problem!")
FIND_PACKAGE(Protobuf REQUIRED)

一旦一切设置完毕,我就可以尝试运行

cmake

mkdir build
cd build
cmake ..

不幸的是,这最终给了我以下错误:

CMake Error at /usr/local/Cellar/cmake/3.23.0/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Protobuf (missing: Protobuf_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.23.0/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.23.0/share/cmake/Modules/FindProtobuf.cmake:650 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  target/CMakeLists.txt:7 (FIND_PACKAGE)

如果有帮助,这里是一些环境信息:

$ uname -a
Darwin coop-mac 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
$ cmake --version
cmake version 3.23.0

protobuf-c
尝试找到
protobuf
,但实际上成功了。为什么从目标搜索找不到它以及如何修复它?

cmake protocol-buffers find-package
2个回答
1
投票

find_package
用于查找已安装 库。已安装的库通常由 cmake 附带的脚本(所谓的 find-modules)找到,或者因为该库提供了配置脚本(如 protobuf-config.cmake 或 profobuf-targets.cmake)。后者是在库构建并安装后生成的。这可能就是您的第二个配置步骤成功的原因。

您不想链接已安装的库,而是自己构建库。因此,您需要做的就是使用

add_subdirectory
添加子文件夹(不要添加 cmake 子文件夹,根据 protobuf,它已被弃用)。之后,您可以使用
target_link_libraries
将 protobuf 链接到您的目标,而不使用
find_package


0
投票

我最近遇到了exact问题:两个子模块调用

find_package(Protobuf)
,其中一个由于“缺少:Protobuf_INCLUDE_DIR”而失败(在第一次运行中)。

启用cmake trace后,我发现了一些警告:

  • 第一天发出的警告
    find_package(Protobuf)
cmake-3.28/Modules/FindProtobuf.cmake(559):  find_path(Protobuf_INCLUDE_DIR google/protobuf/service.h PATHS ${Protobuf_SRC_ROOT_FOLDER}/src )
/root/bin/cmake/share/cmake-3.28/Modules/FindProtobuf.cmake(563):  mark_as_advanced(Protobuf_INCLUDE_DIR )
CMake Warning (dev) at /root/bin/cmake/share/cmake-3.28/Modules/FindProtobuf.cmake:563 (mark_as_advanced):
  Policy CMP0102 is not set: The variable named "Protobuf_INCLUDE_DIR" is not
  in the cache.  This results in an empty cache entry which is no longer
  created when policy CMP0102 is set to NEW.  Run "cmake --help-policy
  CMP0102" for policy details.  Use the cmake_policy command to set the
  policy and suppress this warning.
Call Stack (most recent call first):
  3rdparty/sentencepiece/src/CMakeLists.txt:75 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.
  • 来自第二个 find_package(Protobuf) 的警告:
cmake-3.28/Modules/FindProtobuf.cmake(559):  find_path(Protobuf_INCLUDE_DIR google/protobuf/service.h PATHS ${Protobuf_SRC_ROOT_FOLDER}/src )
CMake Warning (dev) at /root/bin/cmake/share/cmake-3.28/Modules/FindProtobuf.cmake:559 (find_path):
  Policy CMP0126 is not set: set(CACHE) does not remove a normal variable of
  the same name.  Run "cmake --help-policy CMP0126" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning.

  For compatibility with older versions of CMake, normal variable
  "Protobuf_INCLUDE_DIR" will be removed from the current scope.
Call Stack (most recent call first):
  3rdparty/deprecated/sami/lightseq/CMakeLists.txt:36 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

“Protobuf_INCLUDE_DIR”将从当前范围中删除。第二次运行的警告是可疑的。

解决方案

将所有 Protobuf_* 变量设置为缓存变量,例如:

set(Protobuf_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}../deps/protobuf/src" CACHE INTERNAL "Protobuf Include Dir")
© www.soinside.com 2019 - 2024. All rights reserved.