Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake cannot find GoogleTest required library in Ubuntu

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?

like image 825
Erich Avatar asked Jun 18 '14 22:06

Erich


People also ask

How do I know my Gtest version?

Installation Queries: ... --version the version of the Google Test installation Version Queries: --min-version=VERSION return 0 if the version is at least VERSION --exact-version=VERSION return 0 if the version is exactly VERSION --max-version=VERSION return 0 if the version is at most VERSION ...

What is Gtest_main?

It includes a trivial main method that will launch the registered tests, something like this (as of 1.8): GTEST_API_ int main(int argc, char **argv) { printf("Running main() from gtest_main.cc\n"); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }


2 Answers

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.

like image 154
detrick Avatar answered Oct 21 '22 06:10

detrick


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
like image 22
mrts Avatar answered Oct 21 '22 07:10

mrts