12

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?

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
friko
  • 567
  • 2
  • 7
  • 20

2 Answers2

16

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.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • 1
    I have installed gtest headers to /usr/inculde/gtest and libraries to /usr/lib/gtest. So what is the GTEST_ROOT ? I am still confused with this. – friko Jul 04 '13 at 12:30
  • 1
    Assuming you're using [the find script from CMake 2.8.11](http://cmake.org/gitweb?p=cmake.git;a=blob;f=Modules/FindGTest.cmake;h=d531dd12d90fc0bdbb2fba424c448f4e9f69f4bf;hb=0ad0c37206fce114c4e4c31f5270b53e181ab3c2), your `GTEST_ROOT` would have to be `/usr`. That script uses `include` and `lib` as suffixes for include and lib directory respectively, so you should be fine. – ComicSansMS Jul 04 '13 at 12:35
  • export GTEST_ROOT=/usr after that command my cmake still fails with the same reason.. – friko Jul 04 '13 at 12:40
  • 1
    Is your gtest library located in a directory `gtest` under `/usr/lib`? Because that won't work. You need the `libgtest.*` files in `/usr/lib/` directly, not in a subdirectory. – ComicSansMS Jul 04 '13 at 12:42
  • yes, these 2 libs libgtest.a and libgtest_main.a are located under /usr/lib/gtest/ . I will place them to usr/lib and try again. – friko Jul 04 '13 at 12:49
  • Ok, the thing was that the libraries were in /usr/lib/gtest instead of /usr/lib. After coping these lib to proper directory, it works. However, I would like to have all my external libraries (like. gtest, gmock, boost) in separate directories e.g /usr/lib/gtest , /usr/lib/gmock and usr/lib/boost . CMake is failing while looking for those libraries in prior mentioned directories. How to achieve this ? – friko Jul 04 '13 at 13:02
  • In that case you will have to give the path to the libraries manually: `cmake -DGTEST_LIBRARIES:FILEPATH=/usr/lib/gtest/libgtest.a `. Alternatively, you can also enter those values in the interactive prompt of `ccmake` or `cmake-gui`. – ComicSansMS Jul 04 '13 at 13:08
  • Not sure if this helps, but I wanted to "vendor" gtest, so I unpacked it in a vendor subdirectory of my project ("vendor/gtest"). I was setting GTEST_ROOT inside my root CMakeList.txt and then doing find_package inside a subdir CMakeList.txt and was getting the error message described by the OP. I ended up moving find_package into the root CMakeList.txt and then it worked fine. I am a cmake noob so I am not exactly sure what I did wrong, but...HTH someone. – Chris Bilson Aug 09 '13 at 14:47
1

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/

Stan
  • 8,710
  • 2
  • 29
  • 31