简体   繁体   English

Visual Studio 2010如何托管用于C ++项目的MSBuild?

[英]How does Visual Studio 2010 hosts MSBuild for C++ projects?

I have a solution with several C++ projects. 我有几个C ++项目的解决方案。 For some of the projects I need some custom file copy, eg to copy some configuration files to the output directory or to copy the output files of one project to a specific folder after build. 对于某些项目,我需要一些自定义文件副本,例如,将一些配置文件复制到输出目录,或在构建后将一个项目的输出文件复制到特定文件夹。

In some cases I don't want or cannot add these files to the projects directly through the Visual Studio IDE. 在某些情况下,我不希望或者不能直接通过Visual Studio IDE将这些文件添加到项目中。 I created simple .targets files which I can reuse and add to the projects which need the file copying. 我创建了简单的.targets文件,可以将其重用并添加到需要复制文件的项目中。

Here is a simple example .targets file for copying configuration files: 这是一个用于复制配置文件的简单示例.targets文件:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup>
 <BuildDependsOn>
  $(BuildDependsOn);
  CopyCustom
 </BuildDependsOn>
 </PropertyGroup>
 <ItemGroup>
 <CustomFiles Include="$(ProjectDir)Config\**\*.xml" />
 </ItemGroup>
 <PropertyGroup>
 <DestCustFolder>$(OutDir)Config\</DestCustFolder>
 </PropertyGroup> 
 <Target Name="CopyCustom"
 Inputs="@(CustomFiles )"
 Outputs="@(CustomFiles ->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')"> 
 <Message Text="Copy custom files..." />
 <Copy SourceFiles="@(CustomFiles )" DestinationFiles="@(CustomFiles->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')" SkipUnchangedFiles="true" />
 </Target>
</Project>

Through the "Build Customization" dialog in Visual Studio I add it to the project so it will be included like this at the end of the project file: 通过Visual Studio中的“生成自定义”对话框,我将其添加到项目中,以便将其像这样包含在项目文件的末尾:

 <ImportGroup Label="ExtensionTargets">
<Import Project="..\Targets\CopyCustom.targets" />/
 </ImportGroup>

This should enable incremental build of my custom target. 这应该启用我的自定义目标的增量构建。 If I just edit one of my custom files (and none of the C++ files) and build it form the console with 如果我只是编辑一个自定义文件(没有一个C ++文件)并使用以下命令在控制台中进行构建

msbuild foo1.vcxproj

it will actually detect the changes and does an incremental build for my custom target. 它实际上会检测到更改,并为我的自定义目标进行增量构建。 If no changes are made the target is skipped. 如果未进行任何更改,则跳过目标。

If I do however build inside Visual Studio it will not detect changes to the custom files and only and gives me the message that the project is up to data: 但是,如果我确实在Visual Studio中进行构建,它将不会检测到对自定义文件的更改,而只会检测到该项目由数据决定的消息:

========== Build: 0 succeeded, 0 failed, 5 up-to-date, 0 skipped ==========

I would have to additionally change one of the C++ files to make it check all targets again and to the incremental build. 我将不得不另外更改一个C ++文件,以使其再次检查所有目标并进行增量构建。

I was expecting that Visual Studio just executes MSBuild which will then do the up-to-date check on the projects, so it should be the same result as running MSBuild from the console. 我期望Visual Studio只是执行MSBuild,然后对项目进行最新检查,因此它的结果应该与从控制台运行MSBuild的结果相同。 I was trying to get more information by setting the verbosity level to diagnostic but I just get the same line. 我试图通过将详细级别设置为诊断来获取更多信息,但我却得到了相同的结论。 It seems to me that MSBuild is not even executed for the project but Visual Studio itself determines that the project is up-to-date. 在我看来,甚至没有为该项目执行MSBuild,但Visual Studio本身确定该项目是最新的。

So I was wondering how Visual Studio actually determines when it should execute MSBuild for a project. 所以我想知道Visual Studio如何真正确定何时应为项目执行MSBuild。

I asked basically the same question before on the MSDN forum but couldn't get a clear answer. 我之前在MSDN论坛上提出了基本相同的问题,但无法获得明确的答案。

See this suggestion on Microsoft Connect . 在Microsoft Connect上查看此建议

Basically you need to set DisableFastUpToDateCheck property to true to disable the fast-up-to-date check. 基本上,您需要将DisableFastUpToDateCheck属性设置为true以禁用最新的检查。

Just add to your vcxproj or your targets file: 只需添加到您的vcxproj或目标文件中:

<PropertyGroup>  
  <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>

I found an answer by looking into the book "Inside the Microsoft Build Engine, Second Edition". 通过查看《 Inside Microsoft Build Engine,第二版》一书,我找到了答案。

Note: I also updated the same in my question in the MSDN forum but I will mainly duplicate the text here again for completeness. 注意:我在MSDN论坛中的问题中也更新了相同的内容,但是出于完整性的考虑,我将主要在此处再次复制文本。

On page 280 they actually saying that the IDE does a "fast up-to-date check" on the project-level. 在第280页上,他们实际上说IDE在项目级别上进行了“快速最新检查”。 It only spawns a project build and does a more fine-grained check on the individual tasks if this rough project-level check fails. 如果此粗略的项目级检查失败,它只会生成一个项目构建,并对各个任务进行更细粒度的检查。

When running MSBuild from the command line however there is always a fine-grained up-to-date check on the individual tools. 但是,从命令行运行MSBuild时,始终会对各个工具进行最新的细粒度检查。

So the IDE only seems to do this fast check on the files which are added to the projects directly and set as one of the "Input File" types. 因此,IDE似乎只对直接添加到项目中并设置为“输入文件”类型之一的文件进行快速检查。

In my opinion this is not a good design. 我认为这不是一个好的设计。 I would prefer that the IDE is only used to edit the MSBuild project files and then just invokes MSBuild to do the up-to-date check. 我希望IDE仅用于编辑MSBuild项目文件,然后仅调用MSBuild进行最新检查。 This would make it much clearer. 这将使其更加清晰。

I can understand that in a solution with a lot of projects it can make the up-to-date check much faster but there should be at least an option to disable the fast up-to-date check. 我可以理解,在具有很多项目的解决方案中,它可以使最新检查更快,但是至少应该有一个选项可以禁用快速最新检查。 I was checking the IDE if there is a way to disable this behavior but could not find anything. 我正在检查IDE是否可以禁用此行为,但找不到任何东西。

The solution suggested here actually works and I am using it at the moment. 这里建议的解决方案实际上有效,我目前正在使用它。 But I added several custom targets for different kinds of custom files. 但是我为不同种类的自定义文件添加了几个自定义目标。 If I add a new custom file I should never forget to set it to "Custom Build Tool" otherwise the incremental build for this file will not work. 如果添加新的自定义文件,则永远不要忘记将其设置为“自定义构建工具”,否则该文件的增量构建将不起作用。

I guess a solution would be to make a full build customization for my custom files and use specific file extensions for them so Visual Studio will automatically detect them when I add them to the project and sets the right Item Type. 我想解决方案是对自定义文件进行完整的构建自定义,并对它们使用特定的文件扩展名,这样,当我将它们添加到项目中并设置正确的项目类型时,Visual Studio会自动检测它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用msbuild使用2012 C ++运行时库构建Visual Studio 2010项目? - How to use msbuild to build a Visual Studio 2010 project with 2012 C++ runtime libraries? 适用于大型项目的Visual Studio 2010 C ++链接器性能 - Visual Studio 2010 C++ linker performance for large projects C ++项目可以在Visual Studio 2010中使用T4吗? - Can C++ projects use T4 in Visual Studio 2010? Visual Studio 2010:处理多个C ++项目 - Visual Studio 2010: Working with multiple C++ projects 如何配置Visual Studio 2010以为C ++项目构建ANTLR语法? - How can I configure Visual Studio 2010 to build ANTLR grammars for C++ projects? 如何在Visual Studio 2010中使用来自不同C ++项目的函数? - How to use functions from different C++ projects in Visual Studio 2010? Visual Studio C ++ Express 2010-可以与非托管代码一起使用吗? - Visual Studio C++ Express 2010 - does it work with unmanaged code? 用于Visual Studio 2008项目的msbuild - msbuild for visual studio 2008 projects Visual Studio 2010在调用msbuild之前做了什么? - What does Visual Studio 2010 do before invoking msbuild? Visual Studio 2010转换2005解决方案(C ++项目)失去了项目依赖性 - Visual Studio 2010 conversion of 2005 solutions (C++ projects) loses project dependencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM