Why does "#include <gtest/gtest.h>" fail even after having used "FetchContent_MakeAvailable" in my CMake file?

Viewed 526

This is my first time attempting the Google Test API in C++ and one of my first experiences with CMake. If it's useful, I'm using CLion. My CMake file, shown below, should ostensibly allow for files to #include <gtest/gtest.h>:

cmake_minimum_required(VERSION 3.19)
project(chord)

# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)

include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread")
add_executable(
    chord
    main.cpp
    src/node.h
    src/node.cpp
    src/log.h
    src/log.cpp
    src/sha1.h
    src/sha1.cpp
    src/messageParser.h
    src/messageParser.cpp
    src/fingerTable.h
    src/fingerTable.cpp
    src/key.h
    src/key.cpp)
include(GoogleTest)

enable_testing()

add_executable(
    unitTests
    test/fingerTableTests.cc
    test/keyTests.cc
    test/logTests.cc
    test/messageParserTests.cc
    test/nodeTests.cc
    test/messageParserTests.cc
)
target_link_libraries(
    unitTests
    gtest_main
)

include(GoogleTest)
gtest_discover_tests(unitTests)

However, when I attempt #include <gtest/gtest.h> from main.cpp, I receive the error gtest/gtest.h: No such file or directory. Can someone pinpoint the error in my CMake file? Thanks and sorry for the trouble.

1 Answers

Try to also link gtest:

target_link_libraries(
    unitTests
    gtest_main
    gtest
)
Related