如何覆盖Visual Studio 2017社区的编译命令 [英] How to override compile command of Visual Studio 2017 Community

查看:166
本文介绍了如何覆盖Visual Studio 2017社区的编译命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个简单的shell脚本覆盖默认的Visual Studio C ++编译器.我想要的是捕获参数(例如文件名)并创建一些统计信息.但是,我想完全覆盖编译过程-也就是说,我想从我的shell脚本中调用原始编译.

I would like to override the default Visual Studio C++ compiler with a simple shell script. What I want is to capture the arguments, such as file name, and create some statistics. However I want to override the compilation process completely - that is, I want to call the original compilation from my shell script.

我用谷歌搜索,但是发现的只有如何在项目中执行构建前和构建后脚本.那不是我想要的.

I googled but all I found was how to have pre-build and post-build scripts that execute within a project. That's not what I want.

我想全局更改此设置.我该怎么办?

I want to change this globally. How can I do it?

推荐答案

对于标准C ++项目,通过调用MsBuild ,其中列出了实际的使用C ++源文件,可以通过在文本编辑器中打开.vcxproj来轻松查看.因此,此ClCompile项在ClCompile目标中使用,并传递给 CL 任务,而该任务将调用实际编译器可执行文件cl.exe.可以在您使用的工具集的Microsoft.CppCommon.targets文件中找到此代码,以在64位计算机上将VS2017社区默认安装在C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Community \ Common7 \ IDE \ VC \ VCTargets \ Microsoft.CppCommon.targets.

For a standard C++ project file compilation is done by invoking the MsBuild Target named ClCompile. Note there's also an MsBuild Item named ClCompile which lists the actual C++ source files used, this can be readily seen by opening your .vcxproj in a text editor. Consequently this ClCompile Item is used in the ClCompile Target, where it gets passed to the CL Task which in turn will invoke cl.exe, the actual compiler executable. This code for this can be found in the Microsoft.CppCommon.targets file for the toolset you use, for a default install of VS2017 community on a 64bit machine that is C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets.

这3个中的任何一个都可以用自定义版本覆盖,但是正如您所想的那样,最好只替换磁盘上的cl.exe并不是一个好主意.

Any of those 3 could be overridden with a custom version however as you figured already just replacing cl.exe on disk isn't the best idea.

但是CL可以通过重写CLToolExe和CLToolPath来使用任何可执行文件属性.实际上:打开.vcxproj文件并添加

But CL can use any executable simply by overriding the CLToolExe and CLToolPath properties. Practically: open your .vcxproj file and add

<PropertyGroup>
  <CLToolExe>mycl.exe</CLToolExe>
  <CLToolPath>c:\path\to\mycompilerstub\</CLToolPath>
</PropertyGroup>

在导入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.该目录中的任何目标文件都将自动导入.

all the way at the end, after the line importing Microsoft.Cpp.targets; mycl.exe will be called instead of cl.exe. If you want the same effect globally on your machine, you'll put that PropertyGroup in a seperate msbuild file and save it in for example C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\ImportAfter\MyCustomImport.targets. Any targets file in that directory will be imported automatically.

作为替代方案,您可以覆盖ClCompile目标或CL任务.不过,例如,这涉及更多.对于ClCompile,您将从复制Microsoft.CppCommon.targets中找到的整个实现开始,然后添加所需的任何逻辑.优点是您可以直接访问例如源文件等,而无需解析命令行.例如,这将覆盖ClCompile并打印源文件,并将它们传递给自定义可执行文件:

As an alternative you could override the ClCompile target or the CL task. That's more involved though, e.g. for ClCompile you'd start by copying the whole implementation found in Microsoft.CppCommon.targets and then add whatever logic you need. Advantage is you have direct access to e.g. source files etc without having to parse a command line. For example this would override ClCompile and print source files and pass them to a custom executable:

<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目录中,以进行全局覆盖.

Again, this needs to be put at the end of your project file or in the ImportAfter directory for global overriding.

这篇关于如何覆盖Visual Studio 2017社区的编译命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆