使用Visual Studio创建cmake项目 [英] Creating a cmake project with visual studio

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

问题描述

Visual Studio 2017提供了用于处理CMake项目的内置支持. 文档主要涵盖基于先前存在的cmake项目的场景.但是,是否无需创建CMakeLists.txt文件就可以创建cmake项目吗?

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?

推荐答案

VS2017 15.6添加了官方的新建项目CMake向导"

版本15.6 中,从添加新项目"对话框创建CMake项目."

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."

这会创建一个简单的基于C ++的"CMake,您好"项目.

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

您的问题和现有向导的缺乏激发了我写一个问题的机会.这是一个非常基本的设置,如果有更多编写Visual Studio扩展经验的人会有所贡献,那肯定会受益,但这是:

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

编辑:最新的VSIX安装程序现在也可以在VS Marketplace上免费获得

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

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

重新启动Visual Studio 2017之后,新的"CMake可执行模板"将显示在文件/新建/项目/Visual C ++"下:

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:

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

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

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

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

以下是巫师的基本/初始代码作为参考:

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;
        }
    }
}

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

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).

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆