【问题标题】:Creating a cmake project with visual studio使用 Visual Studio 创建一个 cmake 项目
【发布时间】:2018-03-26 07:04:03
【问题描述】:

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

【问题讨论】:

  • 您能否更详细地解释您要查找的内容,例如与"How to set compiler options with CMake in Visual Studio 2017" 相比?因为在我看来,情况正好相反。启用 CMake 的项目上的“打开文件夹”功能删除了 VS 解决方案/项目文件。所以你仍然需要在某个地方定义你想要实际构建的内容和方式。
  • 我希望像“创建 Cmake 项目”这样的东西会打开一个向导来定义构建定义。很像创建 VS2017 Cpp 项目的向导。结果应该是生成的 CMakeList.txt 文件,其中包含基本设置。

标签: c++ visual-studio cmake


【解决方案1】:

编辑:VS2017 15.6 添加了官方的 New Project CMake 向导

version 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

新的“CMake Executable Template”将在“File/New/Project/Visual C++”下重新启动 Visual Studio 2017 后显示:

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

CMakeLists.txt
CMakeSettings.json
MyProject1.cpp 

后续步骤

可能的下一步是:

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

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

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

代码

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

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

参考文献

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 2018-01-27
      相关资源
      最近更新 更多