【问题标题】:Is the ClCompile item a task as well?ClCompile 项也是一项任务吗?
【发布时间】:2018-03-31 19:54:43
【问题描述】:

我正在尝试了解这里的可视化 cpp 项目文档 (https://docs.microsoft.com/en-us/cpp/build/walkthrough-using-msbuild-to-create-a-visual-cpp-project)。

<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  <ItemGroup>  
    <ProjectConfiguration Include="Debug|Win32">  
      <Configuration>Debug</Configuration>  
      <Platform>Win32</Platform>  
    </ProjectConfiguration>  
    <ProjectConfiguration Include="Release|Win32">  
      <Configuration>Release</Configuration>  
      <Platform>Win32</Platform>  
    </ProjectConfiguration>  
  </ItemGroup>  
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />  
  <PropertyGroup>  
    <ConfigurationType>Application</ConfigurationType>  
    <PlatformToolset>v120</PlatformToolset>  
  </PropertyGroup>  
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />  
  <ItemGroup>  
    <ClCompile Include="main.cpp" />  
  </ItemGroup>  
  <ItemGroup>  
    <ClInclude Include="main.h" />  
  </ItemGroup>  
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Targets" />  
</Project>  

我认为 ClCompile 是一个项目,因为它嵌套在 ItemGroup 标记下,似乎要编译的文件也在 ClCompile 标记中引用。在 Items 的文档页面上,https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-items,它声明 Items 是构建系统的输入。我没有看到上面的任务需要这些 ClCompile 项并编译它们,编译是如何实现的? ClCompile 项也是任务吗?

【问题讨论】:

  • 它是一个目标,在 Microsoft.CppCommon.targets 中定义。它所做的最相关的事情是执行 CL 任务。 IDE 也像对 ClInclude 一样关注它,这就是它如何构建在“解决方案资源管理器”窗口中可见的项目树的方式。弄清楚 Microsoft.Cpp.Targets 是如何让这个球滚动起来的,你可能会失去几团头发。

标签: c++ visual-studio visual-c++ msbuild


【解决方案1】:

我没有看到上面的任务需要这些 ClCompile 项并编译它们,编译是如何实现的? ClCompile 项也是任务吗?

在以下路径下的VCTargets文件夹中搜索所有.targets.props文件后:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets

我们可以在Microsoft.CppCommon.targets文件中找到以下代码sn-p:

 <Target Name="ClCompile"
          Condition="'@(ClCompile)' != ''"
          DependsOnTargets="SelectClCompile">

    <PropertyGroup>
      <CLToolArchitecture Condition="'$(CLToolArchitecture)' == ''">$(VCToolArchitecture)</CLToolArchitecture>
      <CLDeleteOutputOnExecute Condition="'$(CLDeleteOutputOnExecute)' == ''">true</CLDeleteOutputOnExecute>
    </PropertyGroup>

    <ItemGroup>
      <ClNoDependencies Condition="'@(ClNoDependencies)' == '' and '%(ClInclude.NoDependency)' == 'true'" Include="@(ClInclude)"/>
      <ClNoDependencies Condition="'$(NoDependencies)' != ''" Include="$(NoDependencies)" />
    </ItemGroup>

    ...

    <OnError Condition="'$(OnXamlPreCompileErrorTarget)' != ''" ExecuteTargets="$(OnXamlPreCompileErrorTarget)" />
  </Target>

所以ClCompile应该是一个target,用来执行Visual C++编译工具,cl.exe编译C/C++源文件。这就是为什么它不在 MSBuild-item 中的原因。

有关更多详细信息,请参阅MSBuild (Visual C++) Overview

【讨论】:

  • 我认为官方文档 (docs.microsoft.com/en-us/cpp/build/…) 确实应该提到 ClCompile 是一个目标,因为它嵌套在 ItemGroup 下使它看起来是一个项目
  • @aileronajay,是的,应该是。但是这个文档是用来谈论如何通过 MSBuild 创建一个 Visual C++ 项目的,它无法解释一些深入但很少注意到的东西。当有人注意到它时,就像你一样。我们会尽力让您清楚。当然,我也会尝试建议 MS 团队根据您的意愿更新此文档。如果以上答案解决了您的疑问,您可以接受它作为答案,这可能对阅读此主题的其他社区成员有所帮助。
【解决方案2】:

是的,CLCompile 实际上运行的是 CLTask 任务类。我怀疑(虽然不确定)他们是这样做的,所以他们可以同时拥有 CLCompile 和 CLInclude 而不必为每个编写任务实现。我不确定在程序集中找到什么命名空间,与纯 .net 语言的任务不同,c++ 任务不在 .net 库文档中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多