'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
Building on linux works fine with gcc
cmake ..
cmake --build .
But building on windows with msvc
"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_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:
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2020 |
