与Cmake一起查找Protobuf包装
我似乎无法始终找到我的 protobuf package package。我想包括Protobuf来源,并将其作为我项目的一部分构建。我正在使用 protobuf 3.15.8 和 protobuf-c-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
root cmakelists.txt
:
cmake_minimum_required(VERSION 3.15)
project(PROTOBUF_IMPORT_FAIL)
add_subdirectory(deps)
add_subdirectory(target)
dep/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 软件包,但实际上是成功的。为什么从目标搜索它找不到它?如何修复它?
I cannot seem to get CMake to consistently find my protobuf package. I would like to include the protobuf source and build it as part of my project. I'm using protobuf 3.15.8 and protobuf-c 1.3.3 (useful as a control example).
This is the directory setup for the example:
- CMakeLists.txt
- deps/
- CMakeLists.txt
- protobuf/ # The protobuf 3.15.8 repository
- protobuf-c/ # The protobuf-c 1.3.3 repository
- target/
- CMakeLists.txt
Root 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)
Once everything is set up I can then try to run cmake
:
mkdir build
cd build
cmake ..
Unfortunately, that eventually gives me the following error:
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)
In case it's helpful, here's some of the environment information:
$ 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
also tries to find the protobuf
package, but it's actually successful. Why does searching for it from the target not find it and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
find_package
用于查找安装库。通常是由装有CMAKE(所谓的Find-Modules)的脚本找到的安装库,或者是因为库提供了一个配置脚本(例如Protobuf-config.cmake.cmake或Profobuf-targets.cmake)。一旦库构建并安装了库后,就会生成后者。这可能就是为什么您的第二个配置步骤成功的原因。您不想链接到已安装的库,而是要自己构建库。因此,您需要做的就是使用
add_subDirectory
添加子文件夹(不要添加Cmake subfolder,根据Protobuf将其弃用)。之后,您可以使用target_link_libraries
无需使用find_package
将Protobuf链接到目标。find_package
is used to find installed libraries. An installed library is normally found by a script shipped with cmake (so called find-modules) or because the library provides a config script (like protobuf-config.cmake or profobuf-targets.cmake). The latter is generated once the library is build and installed. This is possibly why your second configure step is successfull.You don't want to link against the installed library, but build the library by yourself. So all you need to do is add the subfolders using
add_subdirectory
(don't add the cmake subfolder, it's deprecated according to protobuf). After that you can link protobuf against your target usingtarget_link_libraries
without usingfind_package
.我最近遇到了精确问题:两个子模块呼叫
find_package(protobuf)
,其中一个因“缺失:ProtoBuf_include_dir”而失败(在第一次运行中)。启用CMAKE TRACE后,我发现了一些警告:
find_package(ProtoBuf)
的警告:“ ProtoBuf_include_dir”将从当前范围中删除。”第二次跑步的警告是可疑的。
解决方案
将所有ProtoBuf_*变量设置为缓存变量,例如:
I faced exact problem recently: two sub modules call
find_package(Protobuf)
and one of them fails (in the 1st run) due to "missing: Protobuf_INCLUDE_DIR".After enabled cmake trace, I found something warnings:
find_package(Protobuf)
:""Protobuf_INCLUDE_DIR" will be removed from the current scope." warning from the 2nd run is suspicious.
Solution
Set all Protobuf_* variables as cache variables, like: