How to keep source folders hierarchy on solution explorer?

≡放荡痞女 提交于 2019-12-01 06:07:40

Use the source_group command.

source_group(<name> [FILES <src>...] [REGULAR_EXPRESSION <regex>])

Defines a group into which sources will be placed in project files. This is intended to set up file tabs in Visual Studio. The options are:

FILES Any source file specified explicitly will be placed in group . Relative paths are interpreted with respect to the current source directory.

REGULAR_EXPRESSION Any source file whose name matches the regular expression will be placed in group .

@James Adkison is correct; source_group is what you want to use. As of CMake 3.8, the improved source_group command now offers a TREE argument to recursively search your source hierarchy to create source groups to match it. Here is a basic solution for the example you provided:

project(MyProj)

set(MyProj_SOURCES
    "folderA/Toto.cpp"
    "folderA/Tata.cpp"
    "folderB/Foo.cpp"
    "folderB/Bar.cpp"
)

add_executable(Main ${MyProj_SOURCES})

# Create the source groups for source tree with root at CMAKE_CURRENT_SOURCE_DIR.
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${MyProj_SOURCES})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!