使用 Visual Studio 创建一个 cmake 项目

Creating a cmake project with visual studio(使用 Visual Studio 创建一个 cmake 项目)

本文介绍了使用 Visual Studio 创建一个 cmake 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2017 为处理 CMake 项目提供内置支持.

这将创建一个简单的

它在给定的文件夹中生成以下文件,然后在其上使用打开文件夹":

CMakeLists.txtCMakeSettings.json我的项目1.cpp

后续步骤

可能的下一步是:

  • 为一些基本的项目/编译器设置添加一个交互式向导对话框
  • 还添加了一个项目向导,以便能够将源文件添加到 CMakeLists.txt

我期待收到有关基本想法的反馈.请将任何请求直接添加到:

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

代码

这里是威世智的基本/初始代码作为参考:

WizardImplementationClass.cs

//基于 https://docs.microsoft.com/en-us/visualstudio/extensibility/how-to-use-wizards-with-project-templates//和 https://stackoverflow.com/questions/3882764/issue-with-visual-studio-template-directory-creation使用系统;使用 System.IO;使用 System.Collections.Generic;使用 System.Text.RegularExpressions;使用 EnvDTE;使用 Microsoft.VisualStudio.TemplateWizard;使用 Microsoft.VisualStudio.Shell;使用 Microsoft.VisualStudio.Shell.Interop;使用 EnvDTE80;命名空间 VSCMakeWizards{公共类 WizardImplementation : IWizard{公共无效运行开始(对象自动化对象,字典<字符串,字符串>替换字典,WizardRunKind runKind, object[] customParams){var destinationDir = replacementsDictionary["$destinationdirectory$"];var requiredNamespace = replacementsDictionary["$safeprojectname$"];var templatePath = Path.GetDirectoryName((string)customParams[0]);var dte = 自动化对象作为 DTE2;var solution = dte.Solution as EnvDTE100.Solution4;如果(解决方案.IsOpen){解决方案.关闭();}File.Copy(Path.Combine(templatePath, "CMakeSettings.json"), Path.Combine(destinationDir, "CMakeSettings.json"));File.Copy(Path.Combine(templatePath, "main.cpp"), Path.Combine(destinationDir, desiredNamespace + ".cpp"));//参见 https://stackoverflow.com/questions/1231768/c-sharp-string-replace-with-dictionaryRegex re = new Regex(@"($w+$)", RegexOptions.Compiled);string input = File.ReadAllText(Path.Combine(templatePath, "CMakeLists.txt"));字符串输出 = 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;如果(vsSolution!= null){vsSolution.OpenFolder(destinationDir);}抛出新的 WizardCancelledException();}//在打开任何项目之前调用此方法//具有 OpenInEditor 属性.public void BeforeOpeningFile(ProjectItem projectItem){}public void ProjectFinishedGenerating(项目项目){}//该方法只对项目模板调用,//不适用于项目模板.public void ProjectItemFinishedGenerating(ProjectItem项目){}//该方法在项目创建后调用.public void RunFinished(){}//该方法只对项目模板调用,//不适用于项目模板.public bool ShouldAddProjectItem(字符串文件路径){返回假;}}}

注意:WizardCancelledException 是必要的,否则 Visual Studio 会尝试生成/打开实际解决方案.尚不支持打开文件夹"类型的项目向导(没有用于此的 SDK API).

参考资料

  • visual studio 模板问题&创建目录
  • C# 用字典替换字符串

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

解决方案

EDIT: VS2017 15.6 added an official New Project CMake Wizard

With version 15.6 came the feature of "Create CMake projects from the Add New Project dialog."

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

A Custom CMake Wizard

Your question and the lack of an existing Wizard inspired me to write one. It's a very basic setup and would most definitely benefit if people with more experience in writing Visual Studio extensions would contribute, but here it is:

https://github.com/FloriansGit/VSCMakeWizards

Edit: Latest VSIX installer is now also available for free on VS Marketplace

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

The new "CMake Executable Template" will show up after a restart of your Visual Studio 2017 under "File/New/Project/Visual C++":

It generates the following files in the given folder and then uses "Open Folder" on it:

CMakeLists.txt
CMakeSettings.json
MyProject1.cpp 

Next Steps

Possible next steps would be to:

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

I'm looking forward getting feedback on the basic idea. Please add any requests directly to:

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

The Code

And here is the Wizards basic/initial code as a reference:

WizardImplementationClass.cs

// Based on https://docs.microsoft.com/en-us/visualstudio/extensibility/how-to-use-wizards-with-project-templates
//      and https://stackoverflow.com/questions/3882764/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 https://stackoverflow.com/questions/1231768/c-sharp-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: The WizardCancelledException is necessary, because Visual Studio otherwise would try to generate/open an actual solution. An "Open Folder" kind of project wizard is not yet supported (no SDK API for this).

References

  • Issue with visual studio template & directory creation
  • C# String replace with dictionary

这篇关于使用 Visual Studio 创建一个 cmake 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Visual Studio 创建一个 cmake 项目

基础教程推荐