对于标准 C++ 项目文件编译是通过调用 MsBuild 完成的Target https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets named ClCompile
。请注意,还有一个 MsBuildItem https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-items名为 ClCompile,其中列出了实际使用的 C++ 源文件,通过在文本编辑器中打开 .vcxproj 可以很容易地看到这一点。因此,此 ClCompile 项在 ClCompile 目标中使用,并在其中传递给CL https://learn.microsoft.com/en-us/visualstudio/msbuild/cl-task Task https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-tasks这反过来又会调用cl.exe
,实际的编译器可执行文件。此代码可以在您使用的工具集的 Microsoft.CppCommon.targets 文件中找到,对于 64 位计算机上的 VS2017 社区的默认安装,即 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community \Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets。
这 3 个中的任何一个都可以用自定义版本覆盖,但是您已经认为仅替换磁盘上的 cl.exe 并不是最好的主意。
但 CL 可以通过覆盖 CLToolExe 和 CLToolPath 来使用任何可执行文件特性 https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties。实际上:打开 .vcxproj 文件并添加
<PropertyGroup>
<CLToolExe>mycl.exe</CLToolExe>
<CLToolPath>c:\path\to\mycompilerstub\</CLToolPath>
</PropertyGroup>
一直到最后,在 import Microsoft.Cpp.targets 行之后;将调用 mycl.exe 而不是 cl.exe。如果您希望在计算机上全局具有相同的效果,则可以将该 PropertyGroup 放入单独的 msbuild 文件中,并将其保存在例如 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC 中\VCTargets\Platforms\x64\ImportAfter\MyCustomImport.targets。该目录中的任何目标文件都将自动导入。
作为替代方案,您可以覆盖 ClCompile 目标或 CL 任务。但这涉及更多,例如对于 ClCompile,您首先要复制在 Microsoft.CppCommon.targets 中找到的整个实现,然后添加您需要的任何逻辑。优点是您可以直接访问例如源文件等,无需解析命令行。例如,这将覆盖 ClCompile 并打印源文件并将它们传递给自定义可执行文件:
<Target Name="ClCompile"
Condition="'@(ClCompile)' != ''"
DependsOnTargets="SelectClCompile">
<Message Text="All the sources = @(ClCompile)"/>
<Exec Command="mycustom.exe @(ClCompile)" />
... <!--rest of implementation copied from Microsoft.CppCommon.targets goes here-->
</Target>
同样,这需要放在项目文件的末尾或 ImportAfter 目录中以进行全局覆盖。