dimanche 31 mars 2019

Including private headers in tests

I have the following CMake project structure:

mylib
 |
 |_include
 |    |_mylib.h
 |
 |_src
 |  |_linux
 |      |_internal1.h
 |      |
 |      |_internal1.cc
 |      |
 |      |_internal2.h
 |      |
 |      |_internal2.cc
 |      |
 |      |_mylib.c
 |_test
 |   |_all_tests.cc
 |
 |_CMakeLists.txt

My CMakeLists.txt looks as follows:

cmake_minimum_required(VERSION 3.0)

project(mylib)

SET(LIB_SOURCES
    src/linux/internal1.cc
    src/linux/internal2.cc
)

SET(LIB_TEST_SOURCES
    test/all_tests.c
)

add_library(mylib SHARED ${LIB_SOURCES})
target_include_directories(mylib PUBLIC ${PROJECT_SOURCE_DIR}/include)

add_executable(all_tests ${LIB_TEST_SOURCES})
target_include_directories(all_tests PRIVATE src/linux) # <-- HERE?
target_link_libraries(all_tests mylib)

enable_testing()
add_test(NAME all_tests COMMAND all_tests)

The problem: I want to test functions that are not in the
target_include_directories(mylib PUBLIC ${PROJECT_SOURCE_DIR}/include). In my case they are functions defined in internal1.cc and internal2.cc.

Adding target_link_libraries(all_tests mylib) does not add the private headers in the include directory. So the only solution I found currently is to add to include path the whole src/linux directory.

I'm not sure about this approach since it adds h-files as well as c-files.

Is there a better solution for such a case?

Aucun commentaire:

Enregistrer un commentaire