【问题标题】:compile c++ application on c#在 c# 上编译 c++ 应用程序
【发布时间】:2012-01-04 12:05:29
【问题描述】:

我使用以下代码编译一个 C++ 应用程序:

Engine engine = new Engine();
engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319";
BuildPropertyGroup group = new BuildPropertyGroup();
group.SetProperty("Configuration", "Debug");
engine.BuildEnabled = true;
FileLogger logger = new FileLogger();
logger.Parameters = @"logfile=C:\tmp\build.log";
engine.RegisterLogger(logger);
bool success = engine.BuildProjectFile(@"E:\sv_repos\Test\Test\VS2010\Test\Test\Test.vcxproj", new string[] { "Build" }, group);
engine.UnregisterAllLoggers();
if (success)
  MessageBox.Show("build!");

但我收到以下错误,任何想法将不胜感激。

构建开始于 2012/01/04 03:32:16 ب.ظ。 MSBUILD:错误 MSB4014: 由于内部故障,构建被中止。 MSBUILD:错误 MSB4014:System.InvalidCastException:无法转换类型的对象 'System.Xml.XmlComment' 键入 'System.Xml.XmlElement'。 MSBUILD: 错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.ProcessProjectChildren(XmlElement projectElement,字符串 projectDirectoryLocation,布尔值 导入项目)MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.ProcessImportElement(XmlElement importElement,字符串 projectDirectoryLocation,布尔值 导入项目)MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.ProcessProjectChildren(XmlElement projectElement,字符串 projectDirectoryLocation,布尔值 导入项目)MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.ProcessImportElement(XmlElement importElement,字符串 projectDirectoryLocation,布尔值 导入项目)MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.ProcessProjectChildren(XmlElement projectElement,字符串 projectDirectoryLocation,布尔值 导入项目)MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.ProcessMainProjectElement() MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.RefreshProjectIfDirty() MSBUILD: 错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.InternalLoadFromXmlDocument(XmlDocument projectXml, ProjectLoadSettings projectLoadSettings) MSBUILD : 错误 MSB4014:在 Microsoft.Build.BuildEngine.Project.Load(字符串 项目文件名,构建事件上下文构建事件上下文, ProjectLoadSettings projectLoadSettings) MSBUILD:错误 MSB4014:
在 Microsoft.Build.BuildEngine.Engine.GetMatchingProject(项目 现有项目、字符串项目FullPath、BuildPropertyGroup globalPropertiesToUse,字符串工具版本,字符串 [] 目标名称, BuildEventContext buildEventContext,布尔值 toolsVersionPeekedFromProjectFile) MSBUILD:错误 MSB4014:在 Microsoft.Build.BuildEngine.Engine.BuildProjectFileInternal(BuildRequest buildRequest)MSBUILD:错误MSB4014:在 Microsoft.Build.BuildEngine.Engine.EngineBuildLoop(BuildRequest 终止BuildRequest)MSBUILD:错误MSB4014:在 Microsoft.Build.BuildEngine.Engine.PostProjectEvaluationRequests(项目 项目,字符串 [] 项目文件,字符串 [] [] 目标名称, BuildPropertyGroup[] globalPropertiesPerProject, IDictionary[] targetOutputsPerProject, BuildSettings buildFlags, String[] 工具版本)

【问题讨论】:

  • 可以直接在Visual Studio中构建C++项目吗?
  • 可能是 MSBuild 中的一个 bug:XmlComment 不是从 XmlElement 派生的,因此无法强制转换为 XmlElement。
  • 我会查看 .vcxproj 文件内部,看看是否有任何 XML cmets,然后删除它们。
  • 没有xml除了以下:
  • 您找到解决方案了吗?我目前在将Microsoft.Build.BuildEngine.Project.Load 用于在 VS 本身中加载良好的项目时遇到同样的问题。看来问题出在"C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.props"中的cmets@

标签: .net visual-c++ compilation


【解决方案1】:
   engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319";

这是版本控制问题,您的项目使用的是旧版本的 MSBuild 引擎。从堆栈跟踪可见,它使用的是Microsoft.Build.BuildEngine.Project class。哪个有这个属性:

[ObsoleteAttribute("This class has been deprecated. Please use Microsoft.Build.Evaluation.Project from the Microsoft.Build assembly instead.")]

在 .NET 4.0 中对 MSBuild 进行了重大修订,作为 C++ 构建系统大修的一部分。以前使用名为 VCBuild 的旧版构建引擎和旧版项目文件格式,它具有 .vcproj 文件扩展名。您使用的 MSBuild 版本对这些更改一无所知,并且无法正确解析 .vcxproj

您需要更新对 Microsoft.Build.Engine.dll 引用程序集的引用。 “属性”窗口中显示的运行时版本应显示“v4.0.30319”。我在使用 Microsoft.Build.Evaluation.Project 类时运气不佳,它抱怨无法找到 .props 文件并在错误的目录中查找它们。但是它确实可以正确解析 .vcxproj 文件:)

【讨论】:

  • 感谢汉斯的澄清。我确实尝试过使用 Evaluation.Project 也没有运气。我确实遇到了一些其他错误(错误 MSB4127:无法从程序集“...MSBuild\Microsoft.Cpp\v4.0\v120\Microsoft.Build.CppTasks.Common.dll”中实例化“SetEnv”任务。请验证任务程序集是使用相同版本的 Microsoft.Build.Framework 程序集构建的......)这对我来说毫无意义(我刚刚安装了 v120),如果我从 cmd 构建它就像一个魅力。有人成功了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多