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

使用vcpkg部署GTest后CMake遇配置文件查找及版本兼容问题

问题:使用vcpkg管理GTest依赖时CMake find_package失败

初始错误信息

CMake Error at CMakeLists.txt:14 (find_package):
Could not find a package configuration file provided by "GTest" with any of
the following names:

GTestConfig.cmake
gtest-config.cmake

Add the installation prefix of "GTest" to CMAKE_PREFIX_PATH or set
"GTest_DIR" to a directory containing one of the above files. If "GTest"
provides a separate development package or SDK, be sure it has been
installed.

vcpkg安装GTest的步骤

  • 将vcpkg克隆至项目根目录
  • 进入vcpkg目录并运行./vcpkg bootstrap install
  • 执行./vcpkg.exe integrate install
  • 执行./vcpkg.exe install --x-install-root="build"

安装完成后的提示信息:

Total install time: 84.8 ms
The package gtest is compatible with built-in CMake targets:

enable_testing()

find_package(GTest CONFIG REQUIRED)
target_link_libraries(main PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)

add_test(AllTestsInMain main)

当前CMakeLists.txt内容

cmake_minimum_required(VERSION 3.19)
project(Blink LANGUAGES C CXX ASM)
set(CMAKE_CXX_STANDARD 17)

enable_testing()

set(GTest_DIR "/vcpkg/build/x64-windows/share/gtest/")

find_package(GTest CONFIG REQUIRED)
add_executable(sing src/singing-card.cpp inc/singing-card.hpp inc/notes.hpp)
target_include_directories(sing PUBLIC inc)
target_link_libraries(sing PUBLIC ArduinoCore PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)

根据错误提示添加了set(GTest_DIR "/vcpkg/build/x64-windows/share/gtest/")(GTestConfig.cmake确实存在于root\vcpkg\build\x64-windows\share\gtest路径下),但错误依旧。

更新后的新错误

将路径修改为set(GTest_DIR "${CMAKE_CURRENT_LIST_DIR}/vcpkg/build/x64-windows/share/gtest/")后,CMake能找到配置文件,但出现新错误:

CMake Error at CMakeLists.txt:14 (find_package):
Could not find a configuration file for package "GTest" that is compatible
with requested version "".

The following configuration files were considered but not accepted:

.../vcpkg/build/x64-windows/share/gtest/GTestConfig.cmake, version: 1.14.0 (64bit)

疑问

显式指定GTest_DIR后find_package()仍无法正常工作,哪里操作有误?


原因及解决办法

1. 初始路径错误

最初使用的/vcpkg/build/x64-windows/share/gtest/是绝对路径,在Windows环境下会指向系统根目录(如C:\vcpkg\...),而非项目根目录下的vcpkg文件夹,这就是第一次指定路径后仍找不到配置文件的核心原因。改用CMAKE_CURRENT_LIST_DIR修复了路径问题,但触发了版本兼容性问题。

2. 版本兼容性问题

这个错误本质是CMake对GTest配置文件的架构/版本校验失败,可能的原因和解决方式:

  • 指定版本匹配规则
    修改find_package语句,明确接受已安装的GTest版本:

    # 匹配指定版本
    find_package(GTest CONFIG REQUIRED VERSION 1.14.0)
    # 或跳过版本检查
    find_package(GTest CONFIG REQUIRED NO_VERSION_CHECK)
    
  • 升级CMake版本
    GTest 1.14.0推荐使用CMake 3.20及以上版本,将CMake最低版本要求升级:

    cmake_minimum_required(VERSION 3.20)
    
  • 使用vcpkg原生集成方式
    既然已经执行了vcpkg integrate install,完全不需要手动指定GTest_DIR,只需在运行CMake时指定vcpkg的工具链文件即可,vcpkg会自动处理所有依赖的路径和版本:

    cmake .. -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake
    
  • 确认架构一致性
    确保项目编译架构与vcpkg安装GTest的架构一致。如果vcpkg默认安装了x86版本,需指定x64架构重新安装:

    ./vcpkg.exe install gtest:x64-windows --x-install-root="build"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:35:31