CMake Support in Visual Studio – Configuration Templates
Visual Studio 2017 15.8 Preview 3 is now available and it includes several improvements to the CMake tools. In addition to a few fixes we have simplified the way you can configure your CMakeSettings.json file by adding configuration templates.
If you are new to CMake in Visual Studio, check out how to get started.
Configuration Templates for CMake
If you have created a CMakeSettings.json file to customize your project’s settings in the past you might know that that file could be a little daunting. With both the Desktop and Linux workloads installed, the default CMakeSettings.json template was over 100 lines long. We received feedback from the community that this file was difficult to unpack and understand, so we have simplified the way we populate the default CMakeSettings.json file. We have also provided a way to access and edit this configuration file directly from the configuration selector.
When you open a CMake folder for the first time, we now implicitly create just one debug configuration that matches the current operating system. This way, you are still able to get up and running to target the local machine without needing to create a CMakeSettings.json file at all. However, once you do want to add or customize the CMake configuration list, it is easier than ever to do so:
Clicking “Manage Configurations…” in the configuration selection dropdown (right of the F5 button) will ask you to select a template that best fits how you want to debug your project. Visual Studio will add the selected template to a newly created CMakeSettings.json in the root CMakeLists file’s folder.
This has all the configurations we supported before plus a few new ones for targeting MinGW and ARM IoT devices. Once you select a template, it will be added to your CMakeSettings.json file. If we take “x86-Debug” for example, we see that it results in a very compact CMakeSettings.json:
At only 17 lines, this is quite a bit more manageable than this file has been in the past.
Adding New Configurations
Once you create this file, the “Manage Configurations…” button in the configuration selection dropdown will take you back to the CMakeSettings.json file. However, that doesn’t mean you can’t add more templates to it. To add new templates after creating the file just right click anywhere on the editor and select Add Configuration:
Alternatively, you can right click on the file itself in the Solution Explorer:
The newly selected template will be appended to the end of your configuration file.
Custom Configuration Templates (Experimental)
In addition to the built-in configuration templates, we have added an experimental way for you to create your own templates as well. Under Tools > Options > CMake there is now a “CMakeSettings.json Template Directory” entry:
You can point this at a directory with one or more JSON files containing custom templates for CMake. These files can have any name, and essentially have the same format as a standard CMakeSettings.json file. The array of “configurations” will be treated as templates and will appear in the template selector. To set their description in the template sector just add a “description” field to the configuration’s JSON.
We would love to hear how you use this and if it works well for you. Your feedback will help us shape this feature in future versions of Visual Studio.
Community Reported Issues and Other Improvements
We have fixed quite a few customer reported issues in the latest Visual Studio Preview, including several related to our IDE integration with CTest:
- CMake configuration fails with stack trace in Output Window
- CMake + vcpkg problem
- Test Explorer and CTest run from different directories
- C++ Quick Actions Create Definition for a member function returns unknown error
- CMake + Tests: Environment variables ignored
- CMake tests not being detected in 15.6
- CMake: Test Explorer doesn’t escape arguments like CTest
- CMake cache generation gets stuck with Catch2 repository
- Test Explorer does not properly interpret CTest tests with WILL_FAIL property set to TRUE
- CMake View all built tests does not display all CTests
- CMake: Running static code analysis fails to detect c++17
- Visual Studio Build Tools 2017 (15.7.1) missing Visual C++ tools for CMake
In addition to the issues reported by the community, we have also addressed the following customer impacting issues:
- Fixed long delay when closing some CMake projects
- Opening a new CMake project without closing a previously opened project no longer locks up the IDE
- CMake project code scanning no longer waits forever if filters are defined in “.vscode\settings.json”
- C# CMake projects now default to the Visual Studio generator and F5 starts the managed/mixed debugger (experimental)
Thanks again to everyone who reported these issues!
In addition to the changes to CMakeSettings and configuration, the latest version of the CMake tools also includes improved single file compilation. All errors and warnings now appear in the Error List instead of only the Output Window.
Send Us Feedback
Your feedback is a critical part of ensuring that we can deliver the best CMake experience. We would love to know how Visual Studio 2017 Preview is working for you. If you have any feedback specific to CMake Tools, please reach out to firstname.lastname@example.org. For general issues please Report a Problem.