[英]Cmake FetchContent googletest not working on windows
Using FetchContent()
to integrate gtest into project in cmake seems to be missing the relevant include path for gtest/gtest.h
使用FetchContent()
将 gtest 集成到 cmake 中的项目中似乎缺少gtest/gtest.h
的相关包含路径
Building on linux works fine with gcc基于 linux 构建与 gcc 一起工作正常
cmake ..
cmake --build .
But building on windows with msvc但是使用 msvc 在 windows 上构建
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall" x86
cmake -G "Ninja" ..
cmake --build .
Results in:结果是:
Cannot open include file: 'gtest/gtest.h': No such file or directory
fatal error C1083: Cannot open include file: 'gtest/internal/gtest-port.h
Main cmake:主cmake:
cmake_minimum_required(VERSION 3.9)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(VERBOSE ON)
project(test)
option(UNIT_TESTS "Build the unit tests" ON)
if(UNIT_TESTS)
enable_testing()
add_subdirectory(test)
endif()
Here is relevant test cmake:这是相关的测试cmake:
include(FetchContent)
FetchContent_Declare(gtest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG release-1.11.0)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(gtest)
set(project "test_example")
add_executable(${project} example.cpp)
target_link_libraries(${project} gtest_main)
include(GoogleTest)
gtest_discover_tests(${project})
Update更新
Just tested on windows using clang compiler and it works, so seems specific to msvc.刚刚使用 clang 编译器在 windows 上进行了测试,它可以工作,所以似乎特定于 msvc。
Amazingly changing FetchContent_Declare(gtest
to FetchContent_Declare(googletest
fixed this issue. I found this page https://github.com/google/googletest/issues/2457 which seems to be exact same issue as I had.令人惊讶的是,将FetchContent_Declare(gtest
更改为FetchContent_Declare(googletest
解决了这个问题。我发现这个页面https://github.com/google/googletest/issues/2457似乎与我遇到的问题完全相同。
I just ran into the same problem and found a way to solve it.我刚刚遇到了同样的问题,并找到了解决方法。 Although this could be considered a hack.虽然这可以被认为是一种黑客行为。
The problem is that FetchContent_MakeAvailable
creates gtest_SOURCE_DIR
, which points to the root directory of the fetched dependency.问题是FetchContent_MakeAvailable
创建了gtest_SOURCE_DIR
,它指向获取的依赖项的根目录。 The subprojects googletest/googlemock also use gtest_SOURCE_DIR
for adding the include paths (which should be ${gtest_SOURCE_DIR}/googletest
now).子项目 googletest/googlemock 也使用gtest_SOURCE_DIR
添加包含路径(现在应该是${gtest_SOURCE_DIR}/googletest
)。 This name conflict results in missing include directories for the downstream project.此名称冲突导致缺少下游项目的包含目录。
You can fix it by by replacing gtest_SOURCE_DIR
manually.您可以通过手动替换gtest_SOURCE_DIR
来修复它。 This requires using FetchContent_Populate
and add_subdirectory
.这需要使用FetchContent_Populate
和add_subdirectory
。
FetchContent_Declare(
gtest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.12.0
GIT_SHALLOW ON
)
FetchContent_GetProperties(gtest)
if(NOT gtest_POPULATED)
FetchContent_Populate(gtest)
set(TEMP_SOURCE_DIR ${gtest_SOURCE_DIR})
set(gtest_SOURCE_DIR ${gtest_SOURCE_DIR}/googletest)
add_subdirectory(${TEMP_SOURCE_DIR} ${gtest_BINARY_DIR})
endif()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.