Last active
December 6, 2025 05:59
-
-
Save nclettiere/b7075a0858bea54c087d794432953d55 to your computer and use it in GitHub Desktop.
Preserve source code folder structure using CMake and Visual Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Order Visual Studio folder structure | |
| # Put this at the end of your CMake target | |
| # Replace this 'TARGET_SOURCES' with the list of sources (including headers files) of your target. | |
| file(GLOB_RECURSE TARGET_SOURCES CONFIGURE_DEPENDS | |
| ${CMAKE_CURRENT_SOURCE_DIR}/**.cpp | |
| ${CMAKE_CURRENT_SOURCE_DIR}/**/**.cpp | |
| ${CMAKE_CURRENT_SOURCE_DIR}/**.h | |
| ${CMAKE_CURRENT_SOURCE_DIR}/**/**.h | |
| ${CMAKE_CURRENT_SOURCE_DIR}/**.hpp | |
| ${CMAKE_CURRENT_SOURCE_DIR}/**/**.hpp) | |
| foreach(FILE ${TARGET_SOURCES}) | |
| get_filename_component(PARENT_DIR "${FILE}" PATH) | |
| file(RELATIVE_PATH RELATIVE_FILE "${CMAKE_CURRENT_SOURCE_DIR}" "${FILE}") | |
| file(RELATIVE_PATH RELATIVE_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" "${PARENT_DIR}") | |
| string(REPLACE "/" "\\" GROUP "${RELATIVE_PARENT_DIR}") | |
| # Uncomment to separate implementation files and header files | |
| #if ("${RELATIVE_FILE}" MATCHES ".*\\.cpp") | |
| # | |
| #elseif("${RELATIVE_FILE}" MATCHES ".*\\.hpp") | |
| # set(GROUP "Header Files\\${GROUP}") | |
| #elseif("${RELATIVE_FILE}" MATCHES ".*\\.h") | |
| # set(GROUP "Header Files\\${GROUP}") | |
| #endif() | |
| # | |
| # OR | |
| # | |
| # Everything in the same folder... | |
| set(GROUP "Source Files\\${GROUP}") | |
| source_group("${GROUP}" FILES "${RELATIVE_FILE}") | |
| endforeach() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment