7

I'm currently trying to use Microsoft.Build.Execution.BuildManager to build a C++ solution from within a C# winForms application.

From other posts I've come up with the following code, which uses a Logger so I can see some output. The BuildSubmission.BuildResult.OverallResult is always failure when trying to build a C++ application.

string projectFileName = "mySolution.sln";
ProjectCollection pc = new ProjectCollection();
Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Release");
GlobalProperty.Add("Platform", "Win32");

BuildParameters bp = new BuildParameters(pc);
bp.Loggers = new[] {
                      new ConsoleLogger
                      {
                        Verbosity = LoggerVerbosity.Minimal,
                        ShowSummary = true,
                        SkipProjectStartedText = true
                      }
                    };

BuildManager.DefaultBuildManager.BeginBuild(bp);
BuildRequestData BuildRequest = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Clean" }, null);

BuildSubmission BuildSubmission = BuildManager.DefaultBuildManager.PendBuildRequest(BuildRequest);
BuildSubmission.Execute();
BuildManager.DefaultBuildManager.EndBuild();

if (BuildSubmission.BuildResult.OverallResult == BuildResultCode.Failure)
{
    throw new Exception();
}

When I run this code, I get the following output:

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppClean.targets(76,5): error MSB4127: The "CppClean" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Build.CppTasks.Common.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.CPPTasks.CPPClean' to type 'Microsoft.Build.Framework.ITask'.

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppClean.targets(76,5): error MSB4060: The "CppClean" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

Can anyone explain why I'm getting this message an how I can resolve it?

PS. I'd prefer solutions that work with updating an object/variable in the c# code or updates a setting in the c++ solution to those that involve manually updating MSBuild or Visual Studio files. I'm working in an environment with multiple developers and I would prefer not to have to propagate those changes to everyone on the team for this application to work for them.

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Grabanski
  • 363
  • 7
  • 20
  • 1
    Tried to reproduse this and I had to set the third argument to BuildRequestData (toolsversion) to "12.0" else VCTargetsPath was not defined in the loaded project; after that I get a similar exception except that on my machine, which also has VS2013 installed, it looks in *C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppClean.targets*. Funny stuff going on, I'd report this to http://connect.microsoft.com/ – stijn Apr 20 '16 at 07:56
  • @stijn I just saw your comment tried to update ToolsVersion argument for BuildRequestData. First I tried "14.0" since I'm on Visual Studio 2015, to no success, then tried "12.0" just to see and if gave the error error MSB4132: The tools version "12.0" is unrecognized. Available tools versions are "14.0", "2.0", "3.5", "4.0". So seeing that I tried "4.0" and the C++ ran "Clean" with success. but when I tried to run "Build" it failed with error: error MSB8008: Specified platform toolset (v140) is not installed or invalid. Please make sure that a supported PlatformToolset value is selected. – John Grabanski Apr 20 '16 at 12:48
  • Interesting, when I use 14.0 as toolset it gives *error MSB4127: The "SetEnv" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Build.CppTasks.Common.dll"*. Something seems messed up in setting up the environment for the build. – stijn Apr 20 '16 at 13:48
  • using "14.0" gives me the same MSB4127 when trying to run "Build" (This is actually the same error for when I did not have the "Toolsversion" argument set in the BuildRequestData [because I'm running VS2015 (ie "14.0")] Which is weird b/c the C++ solution in question is a VisualStudio 2015 solution – John Grabanski Apr 20 '16 at 18:17
  • 1
    FYI for anyone looking at this thread. I'm currently creating a System.Diagnostics.Process object. calling cmd.exe and running the VsDevCmd.bat and calling MSBuild. The following includes the VS2015 path for the bat file and will clean and rebuild in release `@"/c ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"" x86 && msbuild " + strSolutionName + " /t:clean;rebuild /p:Configuration=Release"` [MSbuild Command Line](https://msdn.microsoft.com/en-us/library/ms164311.aspx) – John Grabanski Apr 25 '16 at 14:11
  • see this answer : http://stackoverflow.com/a/25226782/3205529 , it worked for me, I was using out-dated assembly. – Malick Mar 15 '17 at 16:26

0 Answers0