How to build c++ project with MsBuild v15?

Viewed 1444

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 .

3 Answers

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.

Related