簡體   English   中英

使用visual studio創建一個cmake項目

[英]Creating a cmake project with visual studio

Visual Studio 2017為處理CMake項目提供內置支持。 文檔主要涵蓋基於預先存在的cmake項目的場景。 但有沒有支持創建一個cmake項目而不必擺弄CMakeLists.txt文件?

編輯:VS2017 15.6添加了官方的新項目CMake向導

15.6版本中出現了“從添加新項目對話框創建CMake項目”的功能。

在此輸入圖像描述

這創建了一個簡單的基於的C ++“Hello CMake”項目。

自定義CMake向導

你的問題和缺乏現有的巫師激勵我寫一個。 這是一個非常基本的設置,如果有更多編寫Visual Studio擴展經驗的人會做出貢獻,那么肯定會受益,但這里是:

https://github.com/FloriansGit/VSCMakeWizards

編輯 :最新的VSIX安裝程序現在也可以在VS Marketplace上免費使用

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

在“File / New / Project / Visual C ++”下重新啟動Visual Studio 2017后,將顯示新的“CMake可執行模板”:

CMake項目向導

它在給定文件夾中生成以下文件,然后在其上使用“打開文件夾”:

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否則會嘗試生成/打開實際的解決方案。 尚不支持“打開文件夾”類型的項目向導(沒有SDK API)。

參考

據我所知,沒有向導創建新的CMake項目,但可以通過配置CMakeSettings.json文件來完成。 https://blogs.msdn.microsoft.com/vcblog/2017/08/14/cmake-support-in-visual-studio-customizing-your-environment/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM