我怎样才能让微软C ++编译器对待未知的标记为错误而不是警告? [英] How can I make the Microsoft C++ compiler treat unknown flags as errors rather than warnings?

查看:395
本文介绍了我怎样才能让微软C ++编译器对待未知的标记为错误而不是警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于种种原因,我希望能够脚本是否MS C ++编译器荣誉特定标志的检测。我使用的编译器从Windows 7.1 SDK:

For various reasons, I would like to be able to script the detection of whether the MS C++ compiler honors a particular flag. I'm using the compiler from the Windows 7.1 SDK:

C:\> cl /version
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

因此​​,可以说,我想知道如果标志 / GLEFGB 由该编译器(它不是,因为它不存在)的支持:

So lets say I want to know if the flag /GLEFGB is supported by this compiler (which it is not, because it doesn't exist):

C:\>cl /c ./foo.cc /GLEFBG
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9002 : ignoring unknown option '/GEFBG'
foo.cc

OK,良好的开端,但它是一个警告,它不设置退出状态为无效:

OK, good start, but it is a warning, and it doesn't set the exit status to invalid:

C:\>echo %errorLevel%
0

因此​​,我们应该,如果我们打开警告视为错误与 / WX ,做吧?

C:\>cl /c ./foo.cc /WX /GLEFBG

Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9002 : ignoring unknown option '/GEFBG'
foo.cc

错误。这是越来越令人失望。也许D9002不受 / WX 由于某种原因俘虏?也许我们可以明确地让一个错误使用 /我们​​与code?护理猜测这会不会起作用?

Wrong. This is getting disappointing. Maybe D9002 isn't captured by /WX for some reason? Maybe we can explicitly make it an error by using /we with that code? Care to guess whether this will work?

C:\>cl /c ./foo.cc /WX /weD9002 /GLEFBG
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line error D8021 : invalid numeric argument '/weD9002'

不,现在我们错误,因为显然这个编译器警告标签并不是一个法律论据/我们。我也试过 / we9002 ,这也不行。

Nope, now we error out because apparently the tag for this compiler warning is not a legal argument to /we. I also tried /we9002, which doesn't work either.

所以,现在我的想法。如何说服任何想法 CL 如果传递一个无效的标志和非零退出状态出错了呢?这是真的很难审问为标志的支持编译器没有这种行为。

So, now I am out of ideas. Any thoughts on how to convince cl to error out with a non-zero exit status if passed an invalid flag? It is really hard to interrogate the compiler for flag support without this sort of behavior.

推荐答案

我碰到了同样的问题。而 CL 不报告他们,你也许可以从检查 CL 的输出。

I've run into the same problem. While cl doesn't report them you could perhaps check the output from cl.

我结束了书面执行另一个程序,如果该程序将返回错误或程序标准错误写入任何脚本报告failur Python脚本(即退出code1):

I ended up writing a python script that executes another program and if that program returns error or the program writes anything to stderr the script reports failur (ie exit code 1):

#!/usr/bin/python3
import subprocess
import sys

proc = subprocess.Popen(sys.argv[1:], stderr=subprocess.PIPE)

got_error = False

for l in proc.stderr:
    try:
        print(str(l, errors="replace"), end="")
    except TypeError:
        print(str(l), end="")
    got_error = True

proc.wait()

if got_error or proc.poll() != 0:
    sys.exit(1)

我试过它在Cygwin的,但它应该在dos下运行提示过(也许你将不得不与Python间preTER专门称呼它):

I've tried it under cygwin, but it should work under dos prompt too (perhaps you will have to specifically call it with python interpreter):

python3 chkerr cl /c ./foo.cc /GLEFBG

其中, chkerr 是我给脚本的名称。

这篇关于我怎样才能让微软C ++编译器对待未知的标记为错误而不是警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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