CMake: Exclude Files from Specific Folders (tools, unit_test)
To modify the CMake statement to extract only the .h and .cpp files from all folders except 'tools' and 'unit_test' into the 'hdrs' and 'srcs' variables, you can use the following modified statement:
file(GLOB_RECURSE hdrs
${CMAKE_CURRENT_SOURCE_DIR}/*/*.h*
)
file(GLOB_RECURSE srcs
${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*/*.cc
)
# Exclude files from the 'tools' folder
list(FILTER hdrs EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/tools/")
list(FILTER srcs EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/tools/")
# Exclude files from the 'unit_test' folder
list(FILTER hdrs EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/unit_test/")
list(FILTER srcs EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/unit_test/")
This modified CMake statement will first use the file(GLOB_RECURSE) command to fetch all .h and .cpp files and store them in the 'hdrs' and 'srcs' variables respectively. Then, it will utilize the list(FILTER) command to exclude the files from the 'tools' and 'unit_test' folders.
It's important to note that while using the file(GLOB_RECURSE) command to gather the file list is convenient, it might cause issues with project regeneration in larger projects. For larger projects, manually listing source files is recommended to ensure only required files are included.
For further clarification or assistance with additional inquiries, feel free to ask.
原文地址: https://www.cveoy.top/t/topic/bWtf 著作权归作者所有。请勿转载和采集!