我正在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)
我下载了谷歌的示例代码并将其解压到我的驱动器上。 .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"。
现在,无论我如何修改 CMakeLists.txt 文件,尝试在 Visual Studio Code 中编译该项目都会不断失败。正如提到的那样,我浏览了数十个关于这个问题的主题,并尝试了很多方法,但都没有成功。
更新于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)
编译这个会给我以下结果:
[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