CMake 在编译 Google 的 protobuf 示例时找不到 protobuf
CMake can't find protobuf when compiling Google's protobuf example
我已经尝试解决这个问题 2 天了,但没有成功。我在整个网络上阅读了无数线程并尝试了很多建议,但到目前为止没有运气。
我在 Windows 10 上执行此操作,安装了 VS2017 和最新的 VS Code。我用 vcpkg install protobuf
:
安装了 protobuf
The package protobuf:x64-windows provides CMake targets:
find_package(protobuf CONFIG REQUIRED)
target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
我下载了 Google's example code 并将其解压缩到我的驱动器上。 .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 代码编译项目时,无论我如何修改 CMakeLists.txt 文件,它总是失败。如前所述,我就这个问题浏览了数十个线程,并尝试了很多但没有成功。
2020 年 5 月 29 日更新
我检查了 protobuf 只安装了一次,实际上演示包还包括完整的 protobuf 安装。我用 vcpgk 删除了这个额外的演示包和 un-/installed protobuf。然后我用 protoc(在我的路径中)编译了 .proto 文件并得到了两个文件“addressbook.pb.cc”和“addressbook.pb.h”。
然后我再次尝试编译项目,这次使用的是demo自带的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-config.cmake在protobuf文件夹中可以多次找到:
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 找不到这些文件的原因可能是什么?
公平警告我不是专家。
我 运行 在我自己的构建中遇到了一个类似的问题,试图让 Boost 工作,我认为这与您的环境变量以及您的 Visual Studio 设置方式有关。当您设置重要的东西时,例如
SET(PROTOBUF_INCLUDE_DIR "d:/vcpkg/packages/protobuf_x64-windows/include/")
实际的 find_package(protobuf CONFIG REQUIRED)将这些设置抛出 window。一旦它找到配置文件,它只关心配置文件找到了什么,我认为这就是你的第一个 MESSAGE 是正确的,然后你的第二个 MESSAGE 找不到的原因。
您确定您的机器上只安装了一个 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 选项
祝你好运,抱歉,如果这没有帮助,我对编程还比较陌生,但值得一试。
如果问题出在查找 protobuf 安装上,那么您的 protobuf 路径不在环境变量中,请尝试将其添加到您的路径中,如果存在,请告诉我。
如果这不起作用,我们很乐意为您提供更多帮助
in this thread fraser 解决了这个问题,但是如果你需要根据 protobuf CMake config 和 CMake 中的 find_package 命令进行开发以查找 protobuf 库。你的 protobuf 库必须用 CMake 编译,不要使用配置例程。
使用 CMake 编译 protobuf 后,将在 prefix/lib/CMake/protobuf 目录中生成一个名为 protobuf-config.cmake 的配置文件。
我已经尝试解决这个问题 2 天了,但没有成功。我在整个网络上阅读了无数线程并尝试了很多建议,但到目前为止没有运气。
我在 Windows 10 上执行此操作,安装了 VS2017 和最新的 VS Code。我用 vcpkg install protobuf
:
The package protobuf:x64-windows provides CMake targets:
find_package(protobuf CONFIG REQUIRED) target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
我下载了 Google's example code 并将其解压缩到我的驱动器上。 .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 代码编译项目时,无论我如何修改 CMakeLists.txt 文件,它总是失败。如前所述,我就这个问题浏览了数十个线程,并尝试了很多但没有成功。
2020 年 5 月 29 日更新
我检查了 protobuf 只安装了一次,实际上演示包还包括完整的 protobuf 安装。我用 vcpgk 删除了这个额外的演示包和 un-/installed protobuf。然后我用 protoc(在我的路径中)编译了 .proto 文件并得到了两个文件“addressbook.pb.cc”和“addressbook.pb.h”。
然后我再次尝试编译项目,这次使用的是demo自带的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-config.cmake在protobuf文件夹中可以多次找到:
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 找不到这些文件的原因可能是什么?
公平警告我不是专家。
我 运行 在我自己的构建中遇到了一个类似的问题,试图让 Boost 工作,我认为这与您的环境变量以及您的 Visual Studio 设置方式有关。当您设置重要的东西时,例如
SET(PROTOBUF_INCLUDE_DIR "d:/vcpkg/packages/protobuf_x64-windows/include/")
实际的 find_package(protobuf CONFIG REQUIRED)将这些设置抛出 window。一旦它找到配置文件,它只关心配置文件找到了什么,我认为这就是你的第一个 MESSAGE 是正确的,然后你的第二个 MESSAGE 找不到的原因。
您确定您的机器上只安装了一个 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 选项祝你好运,抱歉,如果这没有帮助,我对编程还比较陌生,但值得一试。
如果问题出在查找 protobuf 安装上,那么您的 protobuf 路径不在环境变量中,请尝试将其添加到您的路径中,如果存在,请告诉我。 如果这不起作用,我们很乐意为您提供更多帮助
in this thread fraser 解决了这个问题,但是如果你需要根据 protobuf CMake config 和 CMake 中的 find_package 命令进行开发以查找 protobuf 库。你的 protobuf 库必须用 CMake 编译,不要使用配置例程。 使用 CMake 编译 protobuf 后,将在 prefix/lib/CMake/protobuf 目录中生成一个名为 protobuf-config.cmake 的配置文件。