13

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.

Kevin
  • 16,549
  • 8
  • 60
  • 74
Javier De Pedro
  • 2,219
  • 4
  • 32
  • 49
  • 1
    Potential duplicate, as there are other similar questions on Stackoverflow. If you want your solution explorer to keep the same folder structure as the file system itself, CMake can support that with [`GLOB_RECURSE`](https://cmake.org/cmake/help/latest/command/file.html#glob-recurse). See this [post](https://stackoverflow.com/questions/9745627/have-cmake-recursively-scan-folders). – Kevin Jul 23 '19 at 12:14
  • I don't think is what I am looking for. – Javier De Pedro Jul 24 '19 at 07:26
  • 1
    *"...I would like to know if there is a way to generate VS solution with the same folder structure that the source code has"* - The post I linked does propose a solution for this. But if it's not what you're looking for, it is unclear what you are asking. Can you please clarify your question? – Kevin Jul 24 '19 at 11:58
  • 3
    The post you linked explains how to recursively scan folders for getting the list of source files in a project. This does not mean that then VS is going to display the list of projects in the same way they are place in the file system. My project is already getting the necessary source files, I don't need to recursively look for them....I just one VS displaying the projects in the same structure. – Javier De Pedro Jul 24 '19 at 12:52
  • 1
    Thanks for the corrections, I am not a native English speaker. – Javier De Pedro Jul 25 '19 at 10:11

1 Answers1

26

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)
Kevin
  • 16,549
  • 8
  • 60
  • 74