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

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

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

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

vcpkg install protobuf
安装了protobuf:

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

find_package(protobuf CONFIG REQUIRED)
target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

我下载了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

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

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


更新2020年5月29日

我检查了 protobuf 仅安装了一次,实际上演示包还包含完整的 protobuf 安装。我删除了这个额外的演示包并使用 vcpkg 卸载/安装了 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)

编译这个给了我:

[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.

在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找不到这些文件可能是什么原因?

c++ cmake protocol-buffers grpc
3个回答
0
投票

公平警告,我不是专家。

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

SET(PROTOBUF_INCLUDE_DIR "d:/vcpkg/packages/protobuf_x64-windows/include/")

实际的 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 选项

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


0
投票

this线程fraser解决了问题,但是如果您需要根据protobuf CMake配置和CMake中的find_package命令进行开发以查找protobuf库。您的 protobuf 库必须使用 CMake 进行编译,并且不要使用配置例程。 使用 CMake 编译 protobuf 后,将在 prefix/lib/CMake/protobuf 目录中生成一个名为 protobuf-config.cmake 的配置文件。


-1
投票

如果问题在于查找 protobuf 安装,那么您的 protobuf 路径不在环境变量中,请尝试将其添加到您的路径中,如果存在,请告诉我。 如果这不起作用,我们很乐意为您提供更多帮助

© www.soinside.com 2019 - 2024. All rights reserved.