How to build c++ project with MsBuild v15?

南楼画角 提交于 2019-12-12 20:32:52

问题


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

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

All is in version 15

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

But i get the below error :

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.

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 .


回答1:


I had to do three main steps:

  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



回答2:


I had similar problem and solved it by adding bindingRedirect to app.config for every msbuild assembly.

Just look at C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config and copy necessary blocks to your app.config.




回答3:


make sure you have below blocks in your C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\msbuild.exe.config

        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Build.Tasks.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.0.0.0"/>
        </dependentAssembly>

Or you may use your own msbuild.exe, make sure you point your env PATH to the right one.



来源:https://stackoverflow.com/questions/44340837/how-to-build-c-project-with-msbuild-v15

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!