1

Let's say I'm part of a team of developers working on an application. The application "lives" on GitHub and we have decided to use CMake because people use different operating systems and editors.

Here is the project structure for the application:

MyProject
├── CMakeLists.txt
├── include
│   └── hello.h
├── src
│   └── hello.cpp
└── build

I'm on Windows and I want to use Visual Studio to work on the application. So I get the codebase from GitHub and run the following commands:

cd build
cmake -G "Visual Studio 17 2022" ..

Then CMake generates a solution (.sln file) for my project which I can open in Visual Studio. Inside Visual Studio, I can build and run my project just fine, but how do I continue work from here?

The solution explorer doesn't show the actual project structure at all - it just uses filters for "Header Files" and "Source Files".

If I add a new header file inside Visual Studio, it is not added to the include folder (where it belongs), it goes inside the build folder.

Additionally, CMakeLists.txt should also be updated when a new file is added. It seems error prone to do so manually every time a new file is added.

Clearly this is not the way to go.

What is the intended workflow for CMake on Windows using Visual Studio?

Is there a good way to work with the generated solution such that files go where they are supposed to and CMakeLists.txt is updated accordingly? How do people generally work on a CMake project on Windows?

Otto
  • 19
  • 2
  • Can you add a folder where you need to first. Then create a filter which follows the same directory structure? Or would that not work/ – Irelia May 02 '23 at 19:59
  • 1
    you shouldn't make any changes in visual studio, just edit the cmake files and re-run cmake (or just build the solution which automatically re-runs cmake). Alternatively don't generate a solution and just open the cmake folder directly with visual studio – Alan Birtles May 02 '23 at 20:05
  • @AlanBirtles Would you still get intellicense if you did that? – Irelia May 02 '23 at 20:07
  • Create a `CMakePresets.json` or `CMakeUserPresets.json` next to the toplevel `CMakeLists.txt`, make sure to provide build dirs that don't overlap and use `Open > CMake...`. Iirc this allows you to use the `Add...` functionality of the solution explorer to modify `CMakeLists.txt`, as long as the files are listed directly in the `add_library`/`add_executable` command. The folder structure in the solution explorer can be modified using the `source_group` command... – fabian May 02 '23 at 20:07
  • @Irelia yes you do – Alan Birtles May 02 '23 at 20:10
  • "_Additionally, CMakeLists.txt should also be updated when a new file is added. It seems error prone to do so manually every time a new file is added._" - No. That's _exactly_ the way it is done and the way it is designed to be done with CMake- unless you use some hacks that everyone (who knows what they are talking about) discourages using. – starball May 02 '23 at 21:29
  • "_The solution explorer doesn't show the actual project structure at all_" - See also https://stackoverflow.com/questions/31422680/how-to-set-visual-studio-filters-for-nested-sub-directory-using-cmake – starball May 02 '23 at 21:31
  • related: [CMake projects in Visual Studio](https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio) – starball May 02 '23 at 21:32
  • `If I add a new header file inside Visual Studio, it is not added to the include folder (where it belongs), it goes inside the build folder.` You can switch to folder view in visual studio and right click the folder-> Add item. – Minxin Yu - MSFT Jun 16 '23 at 08:33

0 Answers0