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 running cmake I get the following error:
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?
You got it backwards. The find_package
call is supposed to find the location of the gtest library for you. You won't need to manually specify the include and library paths anymore:
# 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)
Take a look at the FindGTest.cmake
in your CMake modules directory for details.
The problem why you got the error message is that find_package(GTest REQUIRED)
is unable to find gtest on your system. With the REQUIRED
parameter, you requested CMake to fail immediately if the library cannot be found (which is actually the right thing to do here).
So what you need to do is provide FindGTest
with the means to locate your library. Unfortunately, there is no standard way to do this, as the information needed to find a library varies from library to library. So you will have to check out the source of the find script.
This will tell you that FindGTest
relies on the environment variable GTEST_ROOT
to find the library. Set that environment variable to the path of your gtest installation, re-run CMake and you should be fine.
If your installation's layout differs from the one that FindGTest
expects, you may have to write your own find script instead. The find scripts that ship with CMake are usually quite good, but sometimes they just don't work on certain platforms out-of-the-box. If you can come up with a patch that adds support for your platform, it is usually no problem to get it integrated with the official CMake distribution.
Note that if you intend to build gtest yourself (instead of using the binaries provided by your operating system) using the find script is not the best idea in the first place. You should use an imported target instead.
You need to install the Google Test Framework.
On 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/