mohsen mohsen - 1 year ago 169
C# Question

How to build c++ project with MsBuild v15?

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 .

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download