4

I have just added google-test source code to libs/gtest-1.6.4 directory in my project. There is a libs/gtest-1.6.4/CMakeLists.txt file. In the top-most CMakeLists.txt, I have added add_subdirectory('libs/gtest-1.6.4'). The structure of the project is

|- CMakeLists.txt 
|- src 
   |- CMakeLists.txt 
   |- *.h and *.cc 
|- libs
   |- gtest-1.6.4
      |- CMakeLists.txt
      |- gtest source code etc.
|- other subdirectories 

Now I add #include "gtest/gtest.h" in one of the header file. Compilation fails with

gtest/gtest.h: No such file or directory
compilation terminated.

Here is the snippet of my src/CMakeLists.txt file.

set( Boost_USE_STATIC_LIBS ON )
find_package( Boost COMPONENTS graph regex system filesystem thread REQUIRED)

.. Normal cmake stuff ...
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} )

# This line is added for google-test
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES})

add_executable(Partitioner
  print_function.cc
  methods.cc
  partitioner.cc
  main.cc
  )

TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${GTEST_LIBRARIES})

What am I missing?

Fraser
  • 74,704
  • 20
  • 238
  • 215
Dilawar
  • 5,438
  • 9
  • 45
  • 58
  • Just as an aside (since it doesn't answer your question), have you considered *not* adding GTest sources to your source tree? You can use CMake's [`ExternalProject`](http://www.cmake.org/cmake/help/v2.8.11/cmake.html#module:ExternalProject) module to download and build GTest into your build tree rather than adding 3rd party code to your own source tree. I've got an answer [here](http://stackoverflow.com/a/9695234/2556117) showing how this could be done. – Fraser Aug 04 '13 at 20:47
  • 1
    Yes. But we use private git repositories. We try to keep most of the libraries inside the project so that we don't have to worry about version incompatibilities later. – Dilawar Aug 05 '13 at 04:33
  • 1
    Late to the thread, but just wanted to say that we use a git submodule for gtest to good effect. – Drew Noakes Dec 17 '14 at 16:26

2 Answers2

7

Looking at GTest's CMakeLists.txt, it looks like their include path is ${gtest_SOURCE_DIR}/include. They also define the library as a CMake target called gtest (this is wrapped in a macro cxx_library(gtest ...) currently on line 70).

So it looks like you need to do:

...
# This line is added for google-test
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES})
INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES})
...
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${GTEST_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES} gtest)

You'd also have to ensure that in your root CMakeLists.txt, you've called add_subdirectory(libs/gtest-1.6.4) before add_subdirectory(src) so that the GTest variables are correctly set when they're being used in "src/CMakeLists.txt".

Fraser
  • 74,704
  • 20
  • 238
  • 215
  • I think you really want to use: `target_include_directories` to the google test target in order to find your header files from the `src` directory. Like so: `target_include_directories(thisismy_test PRIVATE ${CMAKE_SOURCE_DIR}/src)` – Melroy van den Berg Jan 23 '22 at 00:40
0

As mentioned in accepted answer,

I did put my GoogleTest stuff up before my add_subdirectory() lines and it worked.

# The ROOT CMakeLists.txt

enable_testing()

include(CTest)

# https://google.github.io/googletest/quickstart-cmake.html
include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/....zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)


add_subdirectory(some)
add_subdirectory(other)
add_subdirectory(another)

And in one of those sub directory, I see tests work.

# A CMakeLists.txt in a sub directory

#enable_testing()    # NOT REQUIRED, hence, commented out
#include(CTest)      # NOT REQUIRED, hence, commented out
#include(GoogleTest) # NOT REQUIRED, hence, commented out

add_executable(
        mytest
        test/mytest.cpp
)
target_link_libraries(
        mytest
        gtest_main
)

gtest_discover_tests(
        mytest
)
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184