如何使用MsBuild v15构建C ++项目? [英] How to build c++ project with MsBuild v15?

查看:123
本文介绍了如何使用MsBuild v15构建C ++项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2017中使用NuGet软件包管理器安装了以下程序集

I installed the folowing assembly with NuGet Package Manager in Visual Studio 2017

Microsoft.Build;
Microsoft.Build.Framework.
Microsoft.Build.Utilities.Core

所有版本均为15

我想用Msbuild构建一个c ++项目

I want to build a c++ project with Msbuild

    public static void Build(string namePerozhe)
    {
        Logger l = new Logger();

        ProjectCollection pc = new ProjectCollection();
        pc.DefaultToolsVersion = "15.0";
        pc.Loggers.Add(l);
        Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
        GlobalProperty.Add("Configuration", "Release");
        GlobalProperty.Add("Platform", "Win32");
        BuildRequestData buildRequest = new BuildRequestData(namePerozhe, GlobalProperty, null, new[] { "Build" }, null);

        BuildParameters buildParameters = new BuildParameters(pc)
        {
            OnlyLogCriticalEvents = false,
            DetailedSummary = true,
            Loggers = new List<Microsoft.Build.Framework.ILogger> { l }.AsEnumerable()
        };

        var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);  
    }

但是我得到以下错误:

无法从程序集中加载"SetEnv"任务 C:\ Program Files \ Microsoft Visual Studio \ 2017 \ Enterprise \ Common7 \ IDE \ VC \ VCTargets \ Microsoft.Build.CppTasks.Common.dll. 无法加载文件或程序集"Microsoft.Build.Utilities.Core,版本= 15.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a"或其依赖项之一. 该系统找不到指定的文件. 确认声明正确无误,并且程序集及其所有依赖项均可用, 并且该任务包含一个实现Microsoft.Build.Framework.ITask的公共类.

The "SetEnv" task could not be loaded from the assembly C:\Program Files\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\ Microsoft.Build.CppTasks.Common.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

当我在Visual Studio中构建此项目时,它将正确构建.但是当我要以编程方式构建它时,会抛出此错误.

when i build this project in visual studio it will build with no error . but when i want to build it programmatically this error will throw up.

还有另一个问题,没有任何答案对我有帮助.

there is another question that don't have any answer that help me .

推荐答案

我必须执行三个主要步骤:

I had to do three main steps:

  1. 从NuGet下载15+版本的Microsoft.Build软件包.不是来自NuGet的版本已经过时.
  2. 下载Microsoft.Build.Runtime程序包.如果您不下载该软件包,则您的项目可能仍会生成,但是它将在运行时在LoadProject上崩溃.
  3. 在我的app.config中连接了bindingRedirects.请参阅: https://developercommunity.visualstudio.com/content/问题/24545/microsoftbuildcpptaskscommon-references-incorrect.html
  1. Download the version 15+ Microsoft.Build packages from NuGet. The ones that aren't from NuGet are out of date.
  2. Download the Microsoft.Build.Runtime package. If you don't download that package, your project might still build, but it'll crash at runtime on LoadProject.
  3. Hooked up bindingRedirects in my app.config. See: https://developercommunity.visualstudio.com/content/problem/24545/microsoftbuildcpptaskscommon-references-incorrect.html

这篇关于如何使用MsBuild v15构建C ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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