How can I make the Microsoft C++ compiler treat unknown flags as errors rather than warnings?

蓝咒 提交于 2019-12-12 14:03:40

问题


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.

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, good start, but it is a warning, and it doesn't set the exit status to invalid:

C:\>echo %errorLevel%
0

So we should be done if we turn on warnings as errors with /WX, right?

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

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'

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.

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.


回答1:


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

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)

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

where chkerr is the name I gave to the script.



来源:https://stackoverflow.com/questions/15259720/how-can-i-make-the-microsoft-c-compiler-treat-unknown-flags-as-errors-rather-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!