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

Avatar

Will

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.

9 comments

Comments are closed. Login to edit/delete your existing comments

  • Avatar
    Hristo Hristov

    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?

    • Avatar
      Will BuikMicrosoft employee

      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.

      • Avatar
        Hristo Hristov

        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.

      • Avatar
        Hristo Hristov

        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.

  • Gerald Wiltse
    Gerald Wiltse

    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
    Gerald Wiltse

    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.

    https://developercommunity.visualstudio.com/idea/921409/add-option-to-remember-view-settings-for-solution.html

    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.