Easily Add, Remove, and Rename Files and Targets in CMake Projects

Will Buik

It’s easier than ever to work with CMake projects in Visual Studio 2019 16.5 Preview 2. Now you can add, remove, and rename source files and targets in your CMake projects from the IDE without manually editing your CMake scripts. When you add or remove files with the Solution Explorer, Visual Studio will automatically edit your CMake project. You can also add, remove, and rename the project’s targets from the Solution Explorer’s targets view.

“Add > New Item” in Solution Explorer’s targets view.

C and C++ Source Files

Visual Studio now tracks C and C++ source files as they are added, renamed, or removed from the Solution Explorer, automatically modifying the underlying CMake project. This feature is enabled by default as of Visual Studio 2019 16.5 Preview 2, but if you would prefer to Visual Studio to not automatically modify your project it can be turned off in Tools > Options > CMake, “Enable automatic CMake script modification…”:

Tools > Options > CMake, “Enable automatic CMake script modification.”

Targets and References

The CMake targets view offers even more functionality. From here, in addition to adding and removing files, you can add, rename, and remove targets. You can access the CMake targets view by clicking on the Solution Explorer’s drop-down menu to the right of the home button:

Access the CMake targets view from the Solution Explorer’s drop-down to the right of the home button.

If you have worked with solutions generated by CMake, this view will look familiar – but unlike a generated solution you will be able to change the underlying CMake project directly in the IDE. Visual Studio currently supports modifying the following:

1. Adding, removing, renaming source files in a target:

CMake Targets View Add Item Menu.

2. Adding, removing, renaming targets in a CMake project:

Add Target Menu.

Add Target Dialog.

3. Viewing and creating references between targets in the project:

Add reference dialog.

CMake references list.

Resolving Ambiguity

In some cases, there may be more than one place where it makes sense to add a source file to a CMake script. When this happens, Visual Studio will ask you where you want to make the change and display a preview of the proposed modifications:

Resolve ambiguity with the preview changes dialog..

Send Us Feedback

Please try out the latest preview and let us know if you have any feedback. It is always appreciated! The best way to get in touch with us about an issue or suggestion is though Developer Community with the “Report a Problem” or “Suggest a Feature” tools. This makes it easy for us to follow up and for you to get the latest updates about our progress. Feel free to comment here or send an email to cmake@microsoft.com with questions as well.