You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows平台VS2022中CMake无法找到已安装的Protobuf包求助

问题描述

在Windows平台的C++项目中尝试使用Protobuf包,已完成以下操作:

  • 从GitHub下载protoc二进制可执行文件
  • 通过vcpkg install protobuf:x64-windows命令完成安装,重复执行该命令提示已安装
  • 在CMakePresets.json中配置了CMAKE_TOOLCHAIN_FILE
  • 清理CMake缓存并重新配置

但在VS2022中使用CMake的find_package指令仍无法找到Protobuf包,报错信息及CMakeLists.txt内容如下:

CMake报错信息

[CMake] -- The C compiler identification is MSVC 19.40.33811.0
1> [CMake] -- The CXX compiler identification is MSVC 19.40.33811.0
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Check for working C compiler: D:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: D:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] CMake Error at D:/Program Files/Microsoft Visual Studio/2022/Community/VC/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package):
1> [CMake]   Could not find a package configuration file provided by "protobuf" with any
1> [CMake]   of the following names:
1> [CMake] 
1> [CMake]     protobufConfig.cmake
1> [CMake]     protobuf-config.cmake
1> [CMake] 
1> [CMake]   Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
1> [CMake]   "protobuf_DIR" to a directory containing one of the above files.  If
1> [CMake]   "protobuf" provides a separate development package or SDK, be sure it has
1> [CMake]   been installed.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   CMakeLists.txt:14 (find_package)
1> [CMake] -- Configuring incomplete, errors occurred!

CMakeLists.txt内容

cmake_minimum_required (VERSION 3.8)

if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("testProtobuf")

find_package(protobuf CONFIG REQUIRED)


add_executable (testProtobuf "testProtobuf.cpp" "testProtobuf.h")

target_link_libraries(testProtobuf PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET testProtobuf PROPERTY CXX_STANDARD 20)
endif()
解决方案
  1. 核对vcpkg工具链配置

    • 检查CMakePresets.json中的CMAKE_TOOLCHAIN_FILE路径是否指向vcpkg根目录下的scripts/buildsystems/vcpkg.cmake,需使用绝对路径,路径含特殊字符时用引号包裹。示例配置:
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "D:/vcpkg/scripts/buildsystems/vcpkg.cmake"
      }
      
    • 打开VS2022的CMake设置界面,确认工具链文件路径与配置一致。
  2. 手动指定protobuf_DIR

    • 在CMakeLists.txt的find_package指令前添加代码,直接指向vcpkg安装的protobuf配置文件目录,替换为你的vcpkg实际路径:
      set(protobuf_DIR "D:/vcpkg/installed/x64-windows/share/protobuf")
      find_package(protobuf CONFIG REQUIRED)
      
  3. 重装并验证protobuf安装文件

    • 执行vcpkg remove protobuf:x64-windows彻底卸载,再重新安装:vcpkg install protobuf:x64-windows
    • 检查vcpkg/installed/x64-windows/share/protobuf目录下是否存在protobufConfig.cmake或protobuf-config.cmake,不存在则说明安装失败,需更新vcpkg后重试。
  4. 同步CMake与vcpkg版本

    • 将CMake升级至3.20以上版本,同时执行vcpkg update和vcpkg upgrade更新vcpkg及已安装包。
  5. 检查VS2022环境与平台匹配

    • 以管理员身份启动VS2022,避免权限导致文件读取失败
    • 确认项目CMake配置中选择的平台为x64,与protobuf安装的x64-windows架构一致。

内容的提问来源于stack exchange,提问作者Shawn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:38:13