Search code examples
visual-studiocmakeqt-creatordirectory-structure

Folder structure for a Visual Studio 2017 with CMake


I am working in a new project in C++ with Qt that is using CMake to generate the solution.

The project is quite big and it's working fine both in Visual Studio 2017 and QtCreator. Most of the people generate the solution for building using Ninja and import the build to QtCreator. But I prefer working with VS IDE.

The problem is that with QtCreator the Solution Explorer is keeping the folder structure, but in VS, all the projects (libs and dlls) hungs up from the solution (root) so I lose some valuable information.

I am quite new in CMake, and I would like to know if there is a way to generate the VS solution with the same folder structure that the source code has without affecting QtCreator solutions.


Solution

  • CMake does support organizing the projects in your Visual Studio Solution Explorer into folders, so you can name the folders to mirror the directory structure on your system. For example, if your projects are organized like this:

    Utilities/LibraryA
    Utilities/LibraryB
    Executables/tools/ParserExecutable
    

    You can use the set_target_properties command with FOLDER to designate the containing folder for each project in the VS Solution Explorer:

    Utilities/CMakeLists.txt:

    set_target_properties(LibraryA PROPERTIES FOLDER "Utilities")
    set_target_properties(LibraryB PROPERTIES FOLDER "Utilities")
    

    Executables/tools/CMakeLists.txt:

    set_target_properties(ParserExecutable PROPERTIES FOLDER "Executables/tools")
    

    You can try to automate this by using CMAKE_CURRENT_SOURCE_DIR instead of naming the folder explicitly, but start with a simple case first!

    Also, make sure you add this to your top-level CMake to enable VS folders for your projects.

    set_property(GLOBAL PROPERTY USE_FOLDERS ON)