FindPackage(GTest CONFIG REQUIRED) 找不到 GTestConfig.cmake,即使它存在

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

我在项目中使用 vcpkg 作为依赖项,并在 CMake 中出现以下错误

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:

  1. 将 vcpkg 克隆到根目录
  2. cd vcpkg
    并运行
    ./vcpkg bootstrap install
  3. ./vcpkg.exe integrate install
  4. ./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
下。但是,我仍然收到相同的错误消息。

我做错了什么,即使明确提供了

find_package()
GTest_DIR
也无法找到配置文件?

更新

我将

set(GTest_DIR "/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)
c++ cmake googletest
1个回答
0
投票

错误

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.

发生的原因是我无论如何都没有向 cmake 提供 vcpkg 工具链文件。如果没有这个文件,

find_package()
将不知道要查看的正确位置。

可以通过命令行参数提供

-DCMAKE_TOOLCHAIN_FILE=<path to vcpkg.cmake>

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