How to use cmake GLOB_RECURSE for only some subdirectories

If you don’t have subdirectories inside “TopDir/File” or “TopDir/Section”, you can do:

file(GLOB ENDF6_SRC
       ${PROJECT_SOURCE_DIR}/*.cpp
       ${PROJECT_SOURCE_DIR}/File/*.cpp
       ${PROJECT_SOURCE_DIR}/Section/*.cpp)

If you do have subdirectories there, you’ll need more than one call:

file(GLOB ENDF6_SRC_TOP
       ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE ENDF6_SRC_NESTED
       ${PROJECT_SOURCE_DIR}/File/*.cpp
       ${PROJECT_SOURCE_DIR}/Section/*.cpp)
set(ENDF6_SRC ${ENDF6_SRC_TOP} ${ENDF6_SRC_NESTED})

By the way, doing file(GLOB_RECURSE ...) in your top-level directory will likely pick up unwanted cpp files from the build folder too in the case of an in-source build (i.e. where the build root is inside “TopDir”).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)