CMake在编译Google的protobuf示例时找不到protobuf

And*_*eas 5 c++ cmake protocol-buffers grpc

我已经尝试解决这个问题 2 天了,但没有成功。我在网络上阅读了无数的帖子并尝试了很多建议,但到目前为止没有运气。

我在安装了 VS2017 和最新 VS Code 的 Windows 10 上执行此操作。我安装了 protobuf vcpkg install protobuf

protobuf:x64-windows 包提供了 CMake 目标:

find_package(protobuf CONFIG REQUIRED)
target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
Run Code Online (Sandbox Code Playgroud)

我下载了Google 的示例代码并将其提取到我的驱动器上。.PROTO 文件编译没有问题:

d:\protobuf-3.12.2\examples>protoc -I=d:\protobuf-3.12.2\examples --cpp_out=d:\protobuf-3.12.2\examples d:\protobuf-3.12.2\examples\addressbook.proto
Run Code Online (Sandbox Code Playgroud)

并按预期创建两个文件“addressbook.pb.cc”和“addressbook.pb.h”。

现在,当我尝试在 Visual Studio Code 中编译项目时,无论我如何修改 CMakeLists.txt 文件,它都会不断失败。如前所述,我浏览了数十个有关此问题的线程,并尝试了很多但没有运气。


更新2020年5月29日

我检查了 protobuf 仅安装了一次,实际上演示包还包含完整的 protobuf 安装。我删除了这个额外的演示包,并使用 vcpgk 卸载/安装了 protobuf。然后,我使用 protoc(位于我的路径中)编译 .proto 文件,并获得两个文件“addressbook.pb.cc”和“addressbook.pb.h”。

然后我尝试再次编译该项目,这次使用演示附带的CMakeLists.txt 。

相关部分一开始似乎是正确的:

# Minimum CMake required
cmake_minimum_required(VERSION 2.8.12)
# Project
project(protobuf-examples)
# Find required protobuf package
find_package(protobuf CONFIG REQUIRED)
if(protobuf_VERBOSE)
  message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")
endif()
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
Run Code Online (Sandbox Code Playgroud)

编译这个给了我:

[main] Building folder: examples 
[main] Configuring folder: examples 
[cms-client] Configuring using the "Visual Studio 15 2017" CMake generator with platform "x64" and toolset "host=x64"
[cmake] Selecting Windows SDK version 10.0.17763.0 to target Windows 
...
[cmake] CMake Error at CMakeLists.txt:8 (find_package):
[cmake]   Could not find a package configuration file provided by "protobuf" with any
[cmake]   of the following names:
[cmake] 
[cmake]     protobufConfig.cmake
[cmake]     protobuf-config.cmake
[cmake] 
[cmake]   Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
[cmake]   "protobuf_DIR" to a directory containing one of the above files.  If
[cmake]   "protobuf" provides a separate development package or SDK, be sure it has
[cmake]   been installed.
[cmake] 
[cmake] 
[cmake] Configuring incomplete, errors occurred!
[cmake] See also "d:/vcpkg/buildtrees/protobuf/src/v3.12.0-8ba83cbbdb/examples/build/CMakeFiles/CMakeOutput.log".
[cms-driver] Error during CMake configure: [cmake-server] Configuration failed.
Run Code Online (Sandbox Code Playgroud)

在 protobuf 文件夹中可以多次找到文件protobuf-config.cmake :

  • D:\vcpkg\buildtrees\protobuf\<BUILDCFG>\share\protobuf\protobuf-config.cmake
  • D:\vcpkg\installed\<BUILDCFG>\share\protobuf\protobuf-config.cmake
  • D:\vcpkg\packages\<BUILDCFG>\share\protobuf\protobuf-config.cmake

CMake 找不到这些文件的原因可能是什么?

小智 0

公平警告我不是专家。

我在自己的构建中遇到了类似的问题,试图让 Boost 正常工作,我认为这与您的环境变量以及 Visual Studio 设置方式有关。当您设置重要的事情时,例如

SET(PROTOBUF_INCLUDE_DIR "d:/vcpkg/packages/protobuf_x64-windows/include/")
Run Code Online (Sandbox Code Playgroud)

实际的 find_package(protobuf CONFIG REQUIRED) 将这些设置扔出窗口。一旦它找到该配置文件,它只关心配置文件正在查找的内容,我认为这就是导致您的第一个消息有正确的消息,然后您的第二个消息找不到的原因。

您确定您的计算机上只安装了一个 protobuf 吗?

  • 似乎发现这个“用作目录D:/protobuf-3.12.2/examples 中的包含目录”
  • 但您正在尝试查找“D:/vcpkg/packages/protobuf_x64-windows”吗?

尝试将“-DCMAKE_PREFIX_PATH="d:/vcpkg/packages/protobuf_x64-windows”添加到 Visual Studio 中的 CMake 选项

祝你好运,如果这没有帮助,我很抱歉,我对编程还比较陌生,但值得一试。