c++visual-studiocmakevisual-studio-2015solution-explorer

How to keep source folders hierarchy on solution explorer?


I made a C++ project on Linux, and I grouped source files in many directories to organize myself.

I was using CMake to compile, with one CMakeFiles.txt on each subdirectory.

srcs
|--folderA
|  |--Toto.cpp
|  |--Tata.cpp
|
|--folderB
|  |--Foo.cpp
|  |--Bar.cpp
[...]

Recently, I opened it with Visual Studio 2015, which found every source file, but just put the entire list on the "Source Files" folder of solution explorer.

Source Files
|--Toto.cpp
|--Tata.cpp
|--Foo.cpp
|--Bar.cpp

I plan to have a huge number of files, and it shall be soon difficult to find one.

Is there any way to explicitly tell it to respect the folder hierarchy on solution explorer?


Solution

  • 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 .