Why does CMake not find GTest (Google Test)?

2019-08-01 16:54发布

问题:

There is a ready project. In one of the cmake-files there is a source code:

find_package(GTest REQUIRED)
if (NOT GTest_FOUND)
    message(FATAL_ERROR "Cannot find Google Test Framework!")
endif()

Gives an error: "Cannot find Google Test Framework!"

How to fix error?

回答1:

The FindGTest.cmake file uses the environment variable GTEST_ROOT. You can add this variable to your system or just add it to your CMakeLists.txt file:

set(GTEST_ROOT "c:/path/to/gtest/root" CACHE PATH "path to gtest"). 

This should solve your problem. It is of course possible to completely add gtest to a project (like Luis Miglietti suggested), but thats maybe not what you want to do as a first try.



回答2:

While CMake provides a FindGTest.cmake module since 2009...

I prefer to incorporate googletest in your CMake project like explaining in the googletest documentation.
https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project

Note: a more detailed explanation http://crascit.com/2015/07/25/cmake-gtest/



回答3:

This could be useful for you so you don't have to depend on a local google test install, this should work independently if you have google test installed in your machine

You can add this to your cmake file (you should take care of the proper linking / include depending on your project structure)

This will download google test, configure the installation and build it in vendor/gtm/gtest (you could always change this) inside your main build folder. Then you can link gtest to an executable so you can run your tests from there

include(ExternalProject)
find_package(Git REQUIRED)

# Build googletest
ExternalProject_Add(
    googletest
    PREFIX "vendor/gtm"
    GIT_REPOSITORY "https://github.com/google/googletest.git"
    GIT_TAG release-1.8.0
    TIMEOUT 10
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    UPDATE_COMMAND ""
)

# Build gtest
ExternalProject_Add(
    gtest_src
    PREFIX "vendor/gtm"
    SOURCE_DIR "vendor/gtm/src/googletest/googletest"
    INSTALL_DIR "vendor/gtm/gtest"
    CMAKE_ARGS
        -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/vendor/gtm/gtest
        -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
        -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
        -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
    DOWNLOAD_COMMAND ""
    UPDATE_COMMAND ""
)

# Prepare gtest
ExternalProject_Get_Property(gtest_src install_dir)
set(GTEST_INCLUDE_DIR ${install_dir}/include)
set(GTEST_LIBRARY_PATH ${install_dir}/lib/libgtest.a)
file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIR})
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION  ${GTEST_LIBRARY_PATH})
set_property(TARGET gtest APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR})

add_dependencies(gtest_src googletest)
add_dependencies(gtest gtest_src)

Then you can link gtest to an executable with something like

add_executable(tester test/tester.cc)
target_link_libraries(tester gtest)
enable_testing()
add_test(<library> tester)