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.


Discussion is closed. Login to edit/delete existing comments.

  • Hristo Hristov 1

    I wonder how this is going to work.
    1. Does VS order the included files alphabetically?
    2. Does it support generator expressions and other means of conditional compilation?

    • Will BuikMicrosoft employee 1

      The behavior used to add new files to the project’s CMake scripts is to find the places where it might make sense to be added and offer to append it to the end of those lists. When there are multiple places where it can be added, for instance on more than one side of a condition, you can choose which one(s) to add the new file to. We do basic dataflow analysis of CMake variables and conditions to find these places.

      Deleted or renamed files are modified in place wherever they are encountered.

      We hope to improve the heuristics for this going forward. If you encounter any issues with it please tell us so we can fix it.

      • Hristo Hristov 1

        I usually put the source files list in a separate *.cmake file which then I #include in my main *.txt file where the targets are defined. The source files added to the variable in an alphabetical order and if they are platform dependent I either use generator expressions or rarely if’s to include them in the list. I haven’t tried the feature yet but I hope it would fit my workflow. Inserting/rearranging the file in an alphabetical order would be very welcome.

      • Hristo Hristov 1

        As expected my use case is not supported. Also I’d prefer if files aren’t automatically added to the CMake file without asking and VS shouldn’t change the formatting of my file. If I have the bracket on a new line it should keep it there.

        • Dwayne Robinson 1

          Oof, if VS reformats my CMake files, then I am most likely turning this off. VS should change only the snippet of text inside the CMakeLists.txt file that needs changing, no more, no less.

          • Will BuikMicrosoft employee 1

            The whole CMake file will not be reformatted, only the directive that needs to be modified. It sounds like VS isn’t matching the formatting of the local snippet quite right in all cases though. We are looking into this, but if you have a specific example, I would encourage you to please open an issue on https://developercommunity.visualstudio.com/.

  • Gerald Wiltse 1

    This is a very nice and big step forward! I requested this feature on uservoice almost 3 years ago, and was uncertain if it would ever be supported. I know the implementation details are likely difficult so I can see it taking some time to build in the flexibility which can accommodate all the ways people want to list their sources. Nonetheless, the fact that a working model got shipped is commendable progress. Thanks.

  • Gerald Wiltse 1

    This post mentions CMake Targets View. CMake Targets view, and features/workflows surrounding it are clearly becoming more prevalent. Thus, I want to raise awareness of the following feature request related to it.


    In short, currently, in order to get to the file tree in CMake Targets View, it’s 5 clicks every single time you open a cmake project/folder. The problem is that VS doesn’t “remember my last view” with respect to anything in the Solution Explorer pane specifically. If “global” support for remembering “all-things-Solution Explorer” can’t be implemented because of scope, maybe the C++/CMake team can implement something specific to remembering just CMake Targets View versus Folder View. That would be acceptable in our case.

  • Devendra Singh 1

    its confusing me to understand..
    I did lots of programming in C++ in Turbo C++ where remove, rename file and manipulate a specific record in a file are all operation possible using codes.
    here is link remove, rename and manipulate specific record in a file C++

    its programs much similar with C++ Turbo Plateform?

Feedback usabilla icon