使用visual studio创建一个cmake项目

时间:2017-10-14 07:04:50

标签: c++ visual-studio cmake

Visual Studio 2017为处理CMake项目提供内置支持。 The documentation主要涵盖基于预先存在的cmake项目的方案。但有没有支持创建一个cmake项目而不必摆弄CMakeLists.txt文件?

2 个答案:

答案 0 :(得分:11)

编辑:VS2017 15.6添加了官方新项目CMake向导

使用version 15.6来自“添加新项目”对话框中的"创建CMake项目的功能。"

enter image description here

这会创建一个简单的基于的C ++" Hello CMake"项目

自定义CMake向导

你的问题和缺乏现有的巫师激励我写一个。这是一个非常基本的设置,如果有更多编写Visual Studio扩展经验的人会做出贡献,那么肯定会受益,但现在是:

https://github.com/FloriansGit/VSCMakeWizards

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

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

新的" CMake可执行模板"将在"文件/新建/项目/ Visual C ++"

下重新启动Visual Studio 2017后显示

CMake Project Wizard

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

CMakeLists.txt
CMakeSettings.json
MyProject1.cpp 

后续步骤

可能的后续步骤是:

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

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

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

守则

以下是Wizards基本/初始代码作为参考:

<强> 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会尝试生成/打开实际的解决方案。一个&#34;打开文件夹&#34;尚不支持某种项目向导(没有SDK API)。

参考

答案 1 :(得分:0)

据我所知,没有向导可以创建新的CMake项目,但可以通过配置CMakeSettings.json文件来完成。 https://blogs.msdn.microsoft.com/vcblog/2017/08/14/cmake-support-in-visual-studio-customizing-your-environment/