Is the ClCompile item a task as well?

2019-08-18 22:13发布

I am trying to understand the visual cpp project document here (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>  

I gather that ClCompile, is an item as it is nested under the ItemGroup tag, it also seems that the files to be compiled are referred to in the ClCompile tag. On the documentation page for Items, https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-items, it states that Items are inputs into the build system. I dont see a task above which takes these ClCompile items and compiles them, how is compilation being achieved? Is the ClCompile item as task as well?

2条回答
趁早两清
2楼-- · 2019-08-18 23:01

I dont see a task above which takes these ClCompile items and compiles them, how is compilation being achieved? Is the ClCompile item as task as well?

After search all the .targets and .props files in the VCTargets folder under following path:

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

We could find following code snippet in the Microsoft.CppCommon.targets file:

 <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>

So ClCompile should be a target, which is used to execute the Visual C++ compiler tool, cl.exe to compile the C/C++ source file. That is reason why it not in the MSBuild-item.

See MSBuild (Visual C++) Overview for some more details.

查看更多
别忘想泡老子
3楼-- · 2019-08-18 23:02

Yes, CLCompile actually runs the CLTask task class. I suspect (though don't know for sure) they did it that way so they could have both CLCompile and CLInclude without having to write task implementations for each. I am not sure what namespace that is found in or the assembly, unlike tasks for pure .net languages the c++ tasks are not in the .net library docs.

查看更多
登录 后发表回答