CMake project directory structure
It’s a good idea to breakup project code into distinct subdirectories. It can be useful to have a CMakeLists.txt for each of these directories that are included from the top-level CMakeLists.txt.
Assume a C project with subdirectory io/ and top-level CMakeLists.txt:
project(benchmarks LANGUAGES C)
enable_testing()
add_subdirectory(io)
add_executable(benchIO bench.c)
target_link_libraries(benchIO PRIVATE iohdf)and in io/CMakeLists.txt:
add_library(iohdf ...)
add_executable(testh5 testh5.c)
target_link_libraries(testh5 PRIVATE iohdf)
add_test(NAME h5io COMMAND testh5)Build a little test script in io/ directory to test just HDF5 I/O,
and compile the whole program from the top level directory, then test the main program and subdirectory libraries:
ctest --test-dir build -VCMake Directory variables: to refer to the parent project directory use ${PROJECT_SOURCE_DIR}.
If using nested projects, ${CMAKE_SOURCE_DIR} refers to the highest level CMakeLists.txt directory
Edit all CMakeLists.txt recursively: on Linux / Unix systems, edit all CMakeLists.txt in a project with a command like
gedit $(find -name CMakeLists.txt)Related: