Search code examples
c++unit-testingcmakegoogletest

CMake GoogleTests can't find header file imported in my test file


We (three students) are very new to CMake. For a university project we have to include GoogleTest into our codebase, and we are having a lot of trouble with it.

We have a gtest_ours.cmake file:

project("googletests")

enable_testing()
include(GoogleTest)

file(GLOB_RECURSE MY_SRC
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
        # header don't need to be included but this might be necessary for some IDEs
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
        EXCLUDE
        "${CMAKE_CURRENT_SOURCE_DIR}/src/MolSim.cpp"
        )

add_subdirectory(googletest)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

add_executable(ParticleContainerTest ${sources} ${CMAKE_CURRENT_SOURCE_DIR}/test/ParticleContainerTest.cpp )
target_link_libraries(ParticleContainerTest ${MY_SRC} gtest gtest_main gmock gmock_main)

gtest_discover_tests(ParticleContainerTest)

set_tests_properties(${noArgsTests}   PROPERTIES TIMEOUT 10)
set_tests_properties(${withArgsTests} PROPERTIES TIMEOUT 20)

and from the CMakeLists.txt file, we just call:

include(gtest_ours)

Our folder structure is:

main
- src
- test
- CMakeLists.txt
- others

Please help, we get the following error:

/home/lunaticcoding/psemoldyn_groupc/MolSim-master/test/ParticleContainerTest.cpp:9:10: fatal error: ../src/ParticleContainer.h: No such file or directory
 #include "../src/ParticleContainer.h"
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/ParticleContainerTest.dir/build.make:62: recipe for target 'CMakeFiles/ParticleContainerTest.dir/test/ParticleContainerTest.cpp.o' failed
make[2]: *** [CMakeFiles/ParticleContainerTest.dir/test/ParticleContainerTest.cpp.o] Error 1
CMakeFiles/Makefile2:144: recipe for target 'CMakeFiles/ParticleContainerTest.dir/all' failed
make[1]: *** [CMakeFiles/ParticleContainerTest.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2

Solution

  • There are a couple issues with the gtest_ours.cmake file. Your error is raised because you haven't defined the include directories for your ParticleContainerTest target to include ParticleContainer.h. You've only added gtest_SOURCE_DIR directories as include directories. To correct this, consider adding this line after add_executable():

    target_include_directories(ParticleContainerTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
    

    This way, the src directory will be an include directory, so you can modify test/ParticleContainerTest.cpp to have a more reasonable #include:

    #include "ParticleContainer.h"
    

    Also, I'm not sure where you intend to use the ${MY_SRC} source files, but you don't want to include this in the target_link_libraries() call. This call should only include pre-defined targets, library names, or linking options as arguments; do not add a list of source files to this call. I'm also not sure where you defined the ${sources} argument in the add_executable() call, but perhaps, you meant to put ${MY_SRC} there instead.


    Finally, the EXCLUDE qualifier does not work with the file(GLOB_RECURSE ...) signature. However, you can remove a specific file from the list afterward, using list(REMOVE_ITEM ...):

    file(GLOB_RECURSE MY_SRC CONFIGURE_DEPENDS
            "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
            # header don't need to be included but this might be necessary for some IDEs
            "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
    )
    list(REMOVE_ITEM MY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/MolSim.cpp)