Googletest: Where to point find_package to find GTest?

Created on 2 Feb 2016  ·  15Comments  ·  Source: google/googletest

I have built GTest as a CMake ExternalProject. Now I want to call
find_package(GTest REQUIRED PATHS ${GTEST_ROOT} NO_DEFAULT_PATH)
to make sure my project uses the GTest that it just built. However, no matter what I set GTEST_ROOT to, find_package complains that it can't find GTestConfig.cmake. I don't see this file anywhere. Can anyone explain how to find a particular GTest build with find_package like this?

Thanks,

David

Most helpful comment

I know this is an old issue, but in case somebody want the solution:

set(GTEST_ROOT "/path/to/gtest" CACHE PATH "Path to googletest")
find_package(GTest REQUIRED)

The reason why your solution does not work is that you provide the PATH to find FindGTest.cmake but that is unnecessary as it is part of your CMake installation. Luckily, FindGTest.cmake look for an environment variable named GTEST_ROOT or a cached variable of the same name, to use as an additional HINT for its search. I use the later here.

Note: "/path/to/gtest" must point to the folder containing the "include/gtest/gtest.h" (usually, where you installed or built it).

All 15 comments

You need to prepare 'finder' for GTest on your own. GTest doesn't come with GTestConfig.cmake. Here is link how to do it: https://cmake.org/Wiki/CMake:How_To_Find_Libraries

Hi,

I'm sorry, I don't follow. Which section of that applies here? GTest DOES have a FindGTest.cmake, and I don't see anything about what to do if there is no GTestConfig.cmake ?

Check 'How package finding works' section in link I gave you. You will need to modify CMAKE_MODULE_PATH

I have read that section, but I still don't see how it applies here? There is already a FindGTest.cmake in the system CMake modules path, so setting CMAKE_MODULE_PATH should not change anything, right? Especially since there is no GTestConfig.cmake to use "config mode"?

  1. Believe me or not but GTest doesn't come with Find* neither *Config.cmake scripts (at least current master branch doesn't have them)
  2. If you have FindGTest.cmake in CMake modules path, then do not specify NO_DEFAULT_PATH
  3. If you have installed GTest manualy then your system wide FindGTest.cmake may not work as it will look for GTest at deafult paths - try to figure out in CMakeCache.txt for GTest related entries and fill them by hand.

If you need more help, you can contact me via gtalk.

CMake has had a FindGTest.cmake since 2009: https://github.com/Kitware/CMake/blob/master/Modules/FindGTest.cmake, which should be in your CMake modules path (most likely in a file called /usr/share/cmake/Modules/FindGTest.cmake). Your problem is more likely with CMake or your OS.

I know this is an old issue, but in case somebody want the solution:

set(GTEST_ROOT "/path/to/gtest" CACHE PATH "Path to googletest")
find_package(GTest REQUIRED)

The reason why your solution does not work is that you provide the PATH to find FindGTest.cmake but that is unnecessary as it is part of your CMake installation. Luckily, FindGTest.cmake look for an environment variable named GTEST_ROOT or a cached variable of the same name, to use as an additional HINT for its search. I use the later here.

Note: "/path/to/gtest" must point to the folder containing the "include/gtest/gtest.h" (usually, where you installed or built it).

GTEST_ROOT does not work for me. I have debian-testing with googletest and cmake 3.9 installed. So there are /usr/share/cmake-3.9/Modules/FindGTest.cmake and /usr/src/googletest/googletest/include/gtest/gtest.h present. Now, if I create the CMakeLists.txt like that in an empty dir

cmake_minimum_required(VERSION 3.9)
project(gtest_find_test)
find_package(GTest)
if(NOT GTEST_LIBRARY)
   message("not found")
endif()

and then run cmake -DGTEST_ROOT=/usr/src/googletest/googletest/ . I get only error

-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)

and the additional line in CMakeLists.txt

set(GTEST_ROOT "/usr/src/googletest/googletest" CACHE PATH "Path to googletest")

does not change anything...

I think in your case the correct GTEST_ROOT is "/usr/src/googletest".

I hope that helps.

On Tue, Nov 7, 2017, 15:42 Денис notifications@github.com wrote:

GTEST_ROOT does not work for me. I have debian-testing with googletest
and cmake 3.9 installed. So there are
/usr/share/cmake-3.9/Modules/FindGTest.cmake and
/usr/src/googletest/googletest/include/gtest/gtest.h present. Now, if I
create the CMakeLists.txt like that in an empty dir

cmake_minimum_required(VERSION 3.9)
project(gtest_find_test)
find_package(GTest)
if(NOT GTEST_LIBRARY)
message("not found")
endif()

and then run cmake -DGTEST_ROOT=/usr/src/googletest/googletest/ . I get
only error

-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)

and the additional line in CMakeLists.txt

set(GTEST_ROOT "/usr/src/googletest/googletest" CACHE PATH "Path to googletest")

does not change anything...


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/google/googletest/issues/702#issuecomment-342661286,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA2Xulkbar-9AZNcsRy5Rvu34zHJV8TJks5s0OrIgaJpZM4HR6Ro
.

But note that googletest must be compiled (source code is not sufficient
for FindGTest).

On Tue, Nov 7, 2017, 19:01 Alexandre Isoard alexandre.isoard@gmail.com
wrote:

I think in your case the correct GTEST_ROOT is "/usr/src/googletest".

I hope that helps.

On Tue, Nov 7, 2017, 15:42 Денис notifications@github.com wrote:

GTEST_ROOT does not work for me. I have debian-testing with googletest
and cmake 3.9 installed. So there are
/usr/share/cmake-3.9/Modules/FindGTest.cmake and
/usr/src/googletest/googletest/include/gtest/gtest.h present. Now, if I
create the CMakeLists.txt like that in an empty dir

cmake_minimum_required(VERSION 3.9)
project(gtest_find_test)
find_package(GTest)
if(NOT GTEST_LIBRARY)
message("not found")
endif()

and then run cmake -DGTEST_ROOT=/usr/src/googletest/googletest/ . I get
only error

-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)

and the additional line in CMakeLists.txt

set(GTEST_ROOT "/usr/src/googletest/googletest" CACHE PATH "Path to googletest")

does not change anything...


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/google/googletest/issues/702#issuecomment-342661286,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA2Xulkbar-9AZNcsRy5Rvu34zHJV8TJks5s0OrIgaJpZM4HR6Ro
.

@aisoard: thanks for the advise! now I came to following "solution"

include(ExternalProject)
set(GOOGLETEST_NAME googletest-distribution)
ExternalProject_Add(${GOOGLETEST_NAME}
  SOURCE_DIR $ENV{GOOGLETEST_DIR}
  CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>")
ExternalProject_Get_Property(${GOOGLETEST_NAME} INSTALL_DIR)

set(GTEST_ROOT ${INSTALL_DIR})
find_package(GTest)
if(NOT GTEST_LIBRARY)
  message(WARNING "gtest not found, call make first to build it and cmake again afterwards")
  return()
endif()

message(STATUS "rest of the build")

it's not a real one, because I need to run cmake twice with make in-between.

Yes, that is a general problem: ExternalProject and find_package do not
work together.

I don't have an other solution (I actually do exactly that). Some people
create a super project that build everything as an external project
(including the actual project).
But that is cumbersome.

On Wed, Nov 8, 2017 at 1:15 PM, Денис notifications@github.com wrote:

@aisoard https://github.com/aisoard: thanks for the advise! now I came
to following "solution"

include(ExternalProject)
set(GOOGLETEST_NAME googletest-distribution)
ExternalProject_Add(${GOOGLETEST_NAME}
SOURCE_DIR $ENV{GOOGLETEST_DIR}
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=")
ExternalProject_Get_Property(${GOOGLETEST_NAME} INSTALL_DIR)

set(GTEST_ROOT ${INSTALL_DIR})
find_package(GTest)
if(NOT GTEST_LIBRARY)
message(WARNING "gtest not found, call make first to build it and cmake again afterwards")
return()
endif()

message(STATUS "rest of the build")

it's not a real one, because I need to run cmake twice with make
in-between.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/google/googletest/issues/702#issuecomment-342961919,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA2Xuh66YkHW4LNTYerOUi2Cc9D2Geicks5s0hnVgaJpZM4HR6Ro
.

--
Alexandre Isoard

I deal with "no library - just sources" by adding build target for them. Here is my finder: https://github.com/Kicer86/photobroom/blob/master/cmake/FindGTest.cmake

Instead of using GTest::GTest you can just add the library yourself...

Here's how I do it:
https://gist.github.com/johnb003/65982fdc7a1274fdb023b0c68664ebe4#file-external_gtest-cmake

I have been cleaning up older and inactive GitHub googletest issues. You may see an issue "closed" if it appears to be inactive/abandoned
Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Joebeazelman picture Joebeazelman  ·  5Comments

marknelson picture marknelson  ·  4Comments

markfrazzetto picture markfrazzetto  ·  3Comments

octoploid picture octoploid  ·  3Comments

maygamboa picture maygamboa  ·  6Comments