Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_package does not find GTest which is part of CMake

I want to find GTest via:

find_package(GTest REQUIRED)

But it is not found:

Error:Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)

I know from this link that GTest should be distributed via standard CMake.

Can you tell me what I did wrong?

like image 934
Roman Volkov Avatar asked Oct 20 '16 07:10

Roman Volkov


Video Answer


1 Answers

If you're on Ubuntu, you should read /usr/share/doc/libgtest-dev/README.Debian. It says:

The Google C++ Testing Framework uses conditional compilation for some things. Because of the C++ "One Definition Rule", gtest must be compiled with exactly the same flags as your C++ code under test. Because this is hard to manage, upstream no longer recommends using precompiled libraries

So you should compile and install your own version of the gtest library with the exactly same Compiler Options, and set the GTEST_LIBRARY or GTEST_ROOT variable accordingly.

For example, I did the following:

$ mkdir -p ExternalLibs/gTest
$ cd ExternalLibs/gTest
$ cmake /usr/src/gtest
$ make

Then I added the following lines in my CMakeLists.txt:

set (GTEST_ROOT ${CMAKE_SOURCE_DIR}/ExternalLibs/gTest)
find_package(GTest REQUIRED)
like image 146
HappyCactus Avatar answered Nov 14 '22 23:11

HappyCactus