37

Similar issue here.

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Add test cpp file
add_executable(foo foo.cpp)

# Link test executable against gtest & gtest_main
target_link_libraries(foo ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)

And my foo.cpp:

#include <gtest/gtest.h>

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Now, all works fine when using the g++ compiler. However, when attempting to use QNX's compiler, ntox86-c++, I run into this problem:

CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE): Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)

I am on Ubuntu using the ntox86-c++ compiler, googletest, and cmake-gui.

What gives?

mrts
  • 16,697
  • 8
  • 89
  • 72
Erich
  • 371
  • 1
  • 3
  • 5
  • 6
    This happened to me when I install gtest but not gtest-devel. On Ubuntu, I'd guess you need something like ``sudo apt-get install libgtest-dev`` – Ami Tavory May 06 '15 at 08:40
  • Can you share the last output before you get the error message. I think it will test for the presence of some header (include) or library. This will help to narrow down the problem. – usr1234567 Oct 08 '15 at 06:15

4 Answers4

53

Google test was probably not properly installed (libgtest-dev may install only source files that needed to be compiled). I had the same problem and I followed the instructions from http://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

sudo apt-get install libgtest-dev
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make

#copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib

This worked for me.

Ivan Kush
  • 2,958
  • 2
  • 23
  • 39
detrick
  • 667
  • 5
  • 6
  • 7
    You should not copy your library into /usr/lib. Instead pass CMake the right flag like GTEST_ROOT or add your GTest directory into CMake's search path. – usr1234567 Oct 08 '15 at 06:16
  • 2
    @detrick forgot to add `cd /usr/src/gtest` before running `sudo cmake CMakeLists.txt` to compile `libgtest-dev` – Conchylicultor Dec 02 '16 at 01:49
  • 5
    Note that you should not copy generated files by hand. Instead, use `sudo make install` – jcmonteiro Jul 29 '19 at 20:29
  • Using Ubuntu 22.04 and CLion it was enough to install libgtest-dev package and reload CMake. Previously I also installed google-mock package, not sure if this one is crucial – Adam Mierzwiak Jul 15 '22 at 14:09
  • maybe a bit late for this but isn't `find_package(GTest REQUIRED)` supposed to download the headers and include those in the cache? why would you need to install `libgtest-dev` by hand? – Jorge Jul 25 '22 at 16:52
24

As explained by @detrick, the Ubuntu package libgtest-dev only installs sources, so you need to build and install the libraries yourself.

However, there is a much simpler way for building and installing since Ubuntu 18.04 than the manual commands in other answers:

sudo apt install libgtest-dev build-essential cmake
cd /usr/src/googletest
sudo cmake .
sudo cmake --build . --target install
mrts
  • 16,697
  • 8
  • 89
  • 72
  • This doesn't work in Ubuntu 16.04. No rule to make target 'install'. – jinge Oct 12 '19 at 09:05
  • @jinge, you are right, this works for Ubuntu 18.04 and newer. I updated the answer accordingly. – mrts Oct 14 '19 at 07:58
  • I tried this on ubuntu 22.04 but also got the 'gmake: *** No rule to make target 'install'. Stop. – zwep Apr 19 '23 at 11:22
  • @zwep, I just tried this on 22.04 and it worked. Did you run `sudo cmake .` before running `sudo cmake --build . --target install`? – mrts Apr 21 '23 at 17:23
4

ntox86-c++ looks like a cross-compiler, libgtest-dev package does not provide compiled library for the target platform (QNX).

Since year 2014 compiled libraries was dropped from libgtest-dev and has been added again in Ubuntu-20.04 focal, so find_package(GTest REQUIRED) does not work on Ubuntu-16.04 xenial and Ubuntu-18.04 bionic. The reason is given in /usr/share/doc/googletest/README.Debian (/usr/share/doc/libgtest-dev/README.Debian) and in e.g. in /usr/src/googletest/googletest/docs/V1_7_FAQ.md "Why is it not recommended to install a pre-compiled copy of Google Test (for example, into /usr/local)" section. Difference in compiler flags for the library and for a test could generate incompatible executable code. The problem with 18.04 and 16.04 is the reason why I have decided to add another answer to the old question.

add_subdirectory could be used to compile gtest provided by system package

set(GTest_ROOT /usr/src/googletest/googletest)
add_subdirectory(${GTest_ROOT}
        "${CMAKE_CURRENT_BINARY_DIR}/googletest" EXCLUDE_FROM_ALL)

add_executable(test test.cpp)
target_link_libraries(test gtest_main)
# or just gtest if main function is defined

Instead of using system package for googletest sources there are at least 2 variants how to obtain particular version from git (besides obvious submodule), see

-3

Some time ago I created a dockerfile and it helps me to keep a kind of recipe for installing later on google test on my systems:

apt-get install -y git g++ make cmake 
git clone https://github.com/google/googletest.git
cd googletest
mkdir gbuild && cd gbuild && cmake .. && make
cp -r googletest/include/gtest /usr/local/include
cp gbuild/googlemock/gtest/lib*.a /usr/local/lib
cp gbuild/googlemock/lib*.a /usr/local/lib

I hope it helps :)