1

I'm trying to use cmake to generate a visual studio project with gtest with the following cmake file..

cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD 11)

find_package(GTest REQUIRED)
message("GTest_INCLUDE_DIRS = ${GTest_INCLUDE_DIRS}")

add_library(commonLibrary LibraryCode.cpp)

add_executable(mainApp main.cpp)
target_link_libraries(mainApp commonLibrary)

add_executable(unitTestRunner testRunner.cpp)
target_link_libraries(unitTestRunner commonLibrary ${GTEST_LIBRARIES} pthread)

I downloaded and compiled gTest at this specific path; C:\Users[MyUserName]\Documents\Libraries\gTest

However when I try to call cmake.. on the cmake file I get met with the following error

  Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR
  GTEST_MAIN_LIBRARY)

What do I need to do to make cmake find these paths?

Mr.Grease
  • 197
  • 1
  • 13
  • FWIW, gtest is a good candidate for just using [`FetchContent()`](https://cmake.org/cmake/help/v3.13/module/FetchContent.html) instead of wrestling with installation paths. It's even the example used in the documentation. –  Nov 22 '21 at 18:37
  • 2
    "I downloaded and compiled gTest at ..." - Have you **installed** GTest after building? For hint CMake about custom installation your could set either `GTEST_ROOT` variable (as described in the [documentation](https://cmake.org/cmake/help/latest/module/FindGTest.html)) or `CMAKE_PREFIX_PATH` variable, as described in [that my answer](https://stackoverflow.com/a/34797156/3440745). – Tsyvarev Nov 22 '21 at 19:02

1 Answers1

0

To be able to use find_package, you must first install googletest manually. On the other hand, FetchContent() will fetch the cmake content from specified source url and the call to FetchContent_MakeAvailable() will make its cmake artifacts to be usable in your CMakeLists.txt.

An example usage would be as follows:

cmake_minimum_required(VERSION 3.14)
include(FetchContent)
FetchContent_Declare(
        googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG        703bd9caab50b139428cea1aaff9974ebee5742e # release-1.10.0
)
FetchContent_MakeAvailable(googletest)

# enable cmake testing
enable_testing()

# add test exe target, and link it with gtest_main library
add_executable(YourTestTarget hello.cc)
target_link_libraries(YourTestTarget gtest_main)

# this will include GoogleTest script and calls test discovery function defined in it, so you donot have to create main function for tests, they will be auto discovered
include(GoogleTest)
gtest_discover_tests(YourTestTarget)

It will fetch the content from googletest git release tag and install it into deps directory in cmake-build folder. Then, its artifacts will be available to your CMakeLists.txt.

Note that, to be able to use FetchContent and FetchContent_MakeAvailable, you need to upgrade cmake at least cmake14.

Aykut Bozkurt
  • 459
  • 5
  • 3
  • 1
    It's worth nothing that `FetchContent_MakeAvailable()` is available as of cmake 3.14 and makes this process a lot cleaner. If OP can change their `cmake_minimum_required()` from 3.13 to at least that version, it would be very much worth their while. –  Nov 22 '21 at 19:07
  • Thanks, this is true, FetchContent_MakeAvailable needs at least cmake3.14. I edit. – Aykut Bozkurt Nov 22 '21 at 19:24