Creating a cmake project with visual studio

Visual Studio 2017 provides built-in support for processing CMake projects. The documentation mainly covers scripts based on pre-existing cmake projects. But is there any support for creating a cmake project without having to mess around with the CMakeLists.txt file?

+5
source share
2 answers

EDIT: VS2017 15.6 official new CMake Wizard added

Since version 15.6 , the function "Create CMake projects from the" Add a new project "dialog box has appeared.

enter image description here

This creates a simple ninja based C ++ Project "Hello CMake".

Custom CMake Wizard

Your question and the lack of an existing Master inspired me to write it. This is a very simple setup and will certainly benefit if people with a lot of experience writing Visual Studio extensions have contributed, but here it is:

https://github.com/FloriansGit/VSCMakeWizards

Edit : The latest VSIX installer is now also available for free on the VS Marketplace

https://marketplace.visualstudio.com/items?itemName=oOFFlianOo.CMakeProjectWizards

The new "CMake Executable Template" will appear after restarting your Visual Studio 2017 in the "File / New / Project / Visual C ++" section:

CMake Project Wizard

It generates the following files in this folder, and then uses "Open Folder" in it:

CMakeLists.txt CMakeSettings.json MyProject1.cpp 

Next steps

Possible next steps:

  • Add an interactive wizard dialog for some basic project / compiler options.
  • Add also the Item Wizard to be able to add source files to CMakeLists.txt

I look forward to receiving feedback on the basic idea. Please add any queries directly:

https://github.com/FloriansGit/VSCMakeWizards/issues

Code

And here is the Wizards base / source code as a reference:

WizardImplementationClass.cs

 // Based on https://docs.microsoft.com/en-us/visualstudio/extensibility/how-to-use-wizards-with-project-templates // and /questions/223022/issue-with-visual-studio-template-directory-creation using System; using System.IO; using System.Collections.Generic; using System.Text.RegularExpressions; using EnvDTE; using Microsoft.VisualStudio.TemplateWizard; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using EnvDTE80; namespace VSCMakeWizards { public class WizardImplementation : IWizard { public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { var destinationDir = replacementsDictionary["$destinationdirectory$"]; var desiredNamespace = replacementsDictionary["$safeprojectname$"]; var templatePath = Path.GetDirectoryName((string)customParams[0]); var dte = automationObject as DTE2; var solution = dte.Solution as EnvDTE100.Solution4; if (solution.IsOpen) { solution.Close(); } File.Copy(Path.Combine(templatePath, "CMakeSettings.json"), Path.Combine(destinationDir, "CMakeSettings.json")); File.Copy(Path.Combine(templatePath, "main.cpp"), Path.Combine(destinationDir, desiredNamespace + ".cpp")); // see /questions/242039/c-string-replace-with-dictionary Regex re = new Regex(@"(\$\w+\$)", RegexOptions.Compiled); string input = File.ReadAllText(Path.Combine(templatePath, "CMakeLists.txt")); string output = re.Replace(input, match => replacementsDictionary[match.Groups[1].Value]); File.WriteAllText(Path.Combine(destinationDir, "CMakeLists.txt"), output); var vsSolution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution7; if (vsSolution != null) { vsSolution.OpenFolder(destinationDir); } throw new WizardCancelledException(); } // This method is called before opening any item that // has the OpenInEditor attribute. public void BeforeOpeningFile(ProjectItem projectItem) { } public void ProjectFinishedGenerating(Project project) { } // This method is only called for item templates, // not for project templates. public void ProjectItemFinishedGenerating(ProjectItem projectItem) { } // This method is called after the project is created. public void RunFinished() { } // This method is only called for item templates, // not for project templates. public bool ShouldAddProjectItem(string filePath) { return false; } } } 

Note. WizardCancelledException necessary because Visual Studio would otherwise try to generate / open the actual solution. Open Folder project wizards are not yet supported (there is no SDK API for this).

References

+5
source

As far as I know, the Wizard does not create a new CMake project, but this can be done by configuring the CMakeSettings.json file. https://blogs.msdn.microsoft.com/vcblog/2017/08/14/cmake-support-in-visual-studio-customizing-your-environment/

0
source

Source: https://habr.com/ru/post/1272612/


All Articles