CMake Support in Visual Studio – Targets View, Single File Compilation, and Cache Generation Settings

Visual Studio 2017 15.7 Preview 3 is now available, which includes several improvements to the CMake tools.  The latest preview offers more control than ever over how to visualize, build, and manage your CMake projects.

Please download the preview and check out the latest CMake features such as the Targets View, single file compilation, and more control over when projects are configured.  As always, we would love to hear your feedback too.

If you are new to CMake in Visual Studio, check out how to get started.

CMake Targets View

The latest preview of Visual Studio offers a new way to visualize your CMake projects’ source and structure.  When you open a CMake project in Visual Studio, you see the project’s layout on disk in the Solution Explorer.  Depending on the way your project is organized, this disk-based view may not be a good reflection of the actual organization of your CMake project.  This is especially true if your project includes files outside of the folder or if it conditionally includes files depending on the active configuration.

The newly added Targets View allows you to visualize the structure of a CMake project in the Solution Explorer.  In this view, source code is organized under individual CMake targets and projects.  You can build and debug individual targets by right clicking them in the Solution Explorer.  You can also see the relationships and dependencies between targets under the References node.  You also have more options to customize the displayed structure of your targets and source code – see Organizing Targets and Source below.

You can show the Targets View by clicking on the view dropdown in the Solution Explorer:

If you have worked with the projects and Solutions generated by CMake before, you should feel right at home.  There is a top-level project node where CMake’s Visual Studio generator would have created a Solution and each CMake target shows up under this project with its source code.  The CMake generator would have created individual MSBuild projects for each of these targets.  One thing to keep in mind, however, is that while the view is similar, no MSBuild projects or Solutions are created.  The view is driven directly by the content of the CMake files.

Organizing Targets and Source

You can also control the organization of the targets and source code.  Targets can be organized by enabling use_folders and setting the folder property for targets.  Source code can be organized under a target using source_groups.  These directives work with all CMake IDE generators (including the Visual Studio generator) so if you already have them set up they will also work with the Targets View.

The Targets View shows a representation of the CMake project’s structure.  Currently, you cannot manipulate this structure from the Targets View.  To modify the project’s structure, you will need to manually modify your project’s CMake list files.

Single File Compilation for CMake Projects

You can now build single files belonging to CMake projects just like you can for MSBuild projects.  Right click on any file in the Solution Explorer and select “Compile” or build the file open in the editor via the main CMake menu:

CMake Cache Generation Settings

Visual Studio automatically configures and generates the cache for your CMake projects by default when you open them for the first time.  This allows the IDE to provide a rich editing, build, and debugging experience, often requiring no additional configuration.  However, we understand that this doesn’t make sense for all projects, so we now offer new settings to control the generation of the CMake project cache:

We recommend sticking with the default, but if you commonly work with projects that require additional configuration or just want more control over how and when Visual Studio generates your CMake project cache, you may want to change this setting.  If you disable automatic generation of the cache, Visual Studio will remind you to generate before editing code belonging to the CMake project:

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 cmake@microsoft.com.  For general issues please Report a Problem.