CMake cannot find the library you need

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.6) # Locate GTest find_package(GTest REQUIRED) include_directories(/usr/include/gtest) # Link runTests with what we want to test and the GTest and pthread library add_executable(runTests gtest.cpp) target_link_libraries(runTests /usr/lib/gtest pthread) 

When cmake starts, the following error appears:

 michael@michaelFriko:~/workspace/gtest/src$ cmake CMakeLists.txt CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (message): Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY) Call Stack (most recent call first): /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:291 (_FPHSA_FAILURE_MESSAGE) /usr/share/cmake-2.8/Modules/FindGTest.cmake:150 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) CMakeLists.txt:8 (find_package) 

How to resolve this?

+8
cmake
source share
2 answers

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 .

+11
source share

You need to install the Google Test Framework.

In Debian / Ubuntu:

 sudo aptitude install libgtest-dev cd /usr/src/gtest/ sudo cmake CMakeLists.txt sudo make sudo cp *.a /usr/lib 

Source: https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

0
source share

All Articles