You got it back. The find_package call should find the location of the gtest library for you. You no longer need to manually specify include paths and libraries:
# Locate GTest find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) # Link runTests with what we want to test and the GTest and pthread library add_executable(runTests my_test.cpp) target_link_libraries(runTests ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)
See FindGTest.cmake in the CMake modules directory for more details.
The problem why you got the error message is that find_package(GTest REQUIRED) cannot find gtest on your system. With the REQUIRED parameter REQUIRED you asked CMake to crash immediately if the library cannot be found (which is actually the right thing to do here).
So, you need to provide FindGTest means to search your library. Unfortunately, there is no standard way to do this, since the information needed to search for a library varies from library to library. Therefore, you will need to check the source of the search script.
This will tell you that FindGTest relies on the GTEST_ROOT environment GTEST_ROOT to find the library. Set this environment variable to your gtest installation path, run CMake, and everything will be fine.
If your installation layout is different than what FindGTest expects, you may have to write your own search script. The search scripts that come with CMake are usually pretty good, but sometimes they just don't work on specific platforms out of the box. If you can come up with a patch that adds support for your platform, there is usually no problem to integrate it with the official CMake distribution.
Please note that if you intend to create gtest yourself (instead of using the binaries provided by your operating system) using find script, this is not a good idea in the first place. Instead, use the imported target .
ComicSansMS
source share