dimanche 12 décembre 2021

How to add include search path to a subdirectory without ’CMakeLists.txt' in CMake?

I have a CMake project with a directory structure below:

.
├── b.h
├── CMakeLists.txt
├── src
└── test
    ├── CMakeLists.txt
    ├── impl
    └── include
        └── a.h

I want to include b.h in a.h but the CMake cannot find b.h in a.h. I know it's wrong if I include a.h in b.h while including b.h in a.h. However, I'm sure that a.h will only be used in files in test/impl.

The content of ./CMakeLists.txt simply adds test/ as a subdirectory.

The content of ./test/CMakeLists generates an executable target which used googletest.

Is there anyway that I can include b.h in a.h without adding an extra CMake file in ./test/include?

./CMakeLists.txt

file(GLOB_RECURSE SOURCES src/*.cpp)

add_library(core SHARED ${SOURCES})
target_include_directories(core PUBLIC ${PROJECT_SOURCE_DIR})

add_subdirectory(test)

./test/CMakeLists.txt

file(GLOB_RECURSE SOURCES impl/*.cpp)

enable_testing()

add_executable(
  core_test
  ${SOURCES}
)
target_link_libraries(
  core_test
  gtest_main
)

include(GoogleTest)
gtest_discover_tests(core_test)

target_include_directories(core_test PUBLIC ${PROJECT_SOURCE_DIR})

Aucun commentaire:

Enregistrer un commentaire