cmake:如何在GTest中使用FetchContent

dw1jzc5e  于 2022-11-11  发布在  其他
关注(0)|答案(3)|浏览(480)

我努力想弄明白先前的帮助:

所以我用gtest编写了一个非常简单的项目用例。
目前我有:

% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(p)

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        58d77fa8070e8cec2dc1ed015d66b454c8d78850 # release-1.12.1
  OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(googletest)

if(TARGET GTest::gtest_main)
  message("Target was found")
endif()

find_package(GTest 1.12.1 REQUIRED)

# Now make sure hwy uses our gtest 1.12.1:

set(HWY_SYSTEM_GTEST ON CACHE BOOL "")
FetchContent_Declare(
  hwy
  GIT_REPOSITORY https://github.com/google/highway.git
  GIT_TAG        22e3d7276f4157d4a47586ba9fd91dd6303f441a # 1.0.1
)
FetchContent_MakeAvailable(hwy)

如果我运行它,会得到以下结果:

$ cmake ..
[...]
Target was found
-- Found GTest: /usr/lib/x86_64-linux-gnu/libgtest.a (Required is at least version "1.12.1")
[...]
-- Found GTest: /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake (found version "1.10.0")

输出对我来说完全没有意义。我想在构建hwy 1.0.1的过程中重新注入googletest 1.12.1的构建树。我的问题如下:在上面的例子中,我应该如何做呢?
原始的上游文档对于将FetchContent_Declare与find_package()集成非常不清楚。
问题的重点是下面两行:


# Now make sure hwy uses our gtest 1.12.1:

set(HWY_SYSTEM_GTEST ON CACHE BOOL "")

将gtest重新注入到hwy的机制。我的问题不是关于通过fetchcontent简单直接使用gtest。

mitkmikd

mitkmikd1#

官方文件说:
当FetchContent_Declare(...)调用包含此选项时,对find_package(...)的后续调用将确保已调用FetchContent_MakeAvailable()
FetchContent_Declarefind_package使用了不同的名称。使用find_package(googletest 1.12.1 REQUIRED)即可。
不幸的是,googletest附带的cmake脚本中有一个“bug”,所以你不能将GTestFetchContent_Declare一起使用,它将GTest_FOUNDGMock_FOUND用于子项目,因此你不能将GTest用于父项目(名称冲突)。

vhipe2zx

vhipe2zx2#

以下是适合我的方法(使用cmake 3.24.1)

cmake_minimum_required(VERSION 3.24)
project(p)

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG 58d77fa8070e8cec2dc1ed015d66b454c8d78850 # release-1.12.1
  OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(googletest)

if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
   AND NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfig.cmake)
  file(
    WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
    [=[
include(CMakeFindDependencyMacro)
find_dependency(googletest)
if(NOT TARGET GTest::GTest)
  add_library(GTest::GTest INTERFACE IMPORTED)
  target_link_libraries(GTest::GTest INTERFACE GTest::gtest)
endif()
if(NOT TARGET GTest::Main)
  add_library(GTest::Main INTERFACE IMPORTED)
  target_link_libraries(GTest::Main INTERFACE GTest::gtest_main)
endif()
]=])
endif()

if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
   AND NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfigVersion.cmake)
  file(
    WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
    [=[
include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-config-version.cmake OPTIONAL)
if(NOT PACKAGE_VERSION_COMPATIBLE)
  include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestConfigVersion.cmake OPTIONAL)
endif()
]=])
endif()

FetchContent_Declare(
  hwy
  GIT_REPOSITORY https://github.com/google/highway.git
  GIT_TAG 22e3d7276f4157d4a47586ba9fd91dd6303f441a # 1.0.1
)

# Now make sure hwy uses our gtest 1.12.1:

set(HWY_SYSTEM_GTEST
    ON
    CACHE BOOL "")
FetchContent_MakeAvailable(hwy)

仅供参考,hwy现在支持新样式的gtest,因为提交:

ddrv8njm

ddrv8njm3#

下面的代码对我有用:
CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(case23)

include(FetchContent)
FetchContent_Declare(googletest
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG release-1.12.1
)
FetchContent_MakeAvailable(googletest)

add_executable(main main.cpp)
target_link_libraries(main gtest_main)

main.cpp


# include <gtest/gtest.h>

int myadd(int a, int b) {
    return a + b;
}

TEST(testCase, test1) {
    EXPECT_EQ(myadd(2, 3), 5);
}

相关问题