How can I get the Microsoft C ++ compiler to treat unknown flags as errors, not warnings?

For various reasons, I would like to be able to determine if the MS C ++ compiler respects a specific flag. I am 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 let's say I want to know if this /GLEFGB flag is /GLEFGB this compiler (which is not because it does not 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, a good start, but this is a warning and it does not set the exit status to void:

 C:\>echo %errorLevel% 0 

So, we have to do this if we enable warnings as errors using /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 disappointing. Maybe the D9002 does not fall under /WX for some reason? Maybe we can explicitly make this a mistake using /we with this code? Guess if 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' 

No, now we are mistaken, because, apparently, the tag for this compiler warning is not a legal argument for /we . I also tried /we9002 , which also does not work.

So now I have no ideas. Any thoughts on how to convince cl of an error with non-zero exit status if an invalid flag is passed? It is very difficult to interrogate the compiler to support the flag without this behavior.

+3
source share
1 answer

I ran into the same problem. As long as cl doesn't report them, you can check the output with cl .

As a result, I wrote a python script that runs another program, and if this program returns an error or the program writes something to stderr , the script message reports a failure (i.e. 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 tried this under cygwin, but it should work under the dos prompt too (you may have to specifically call it using the python interpreter):

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

where chkerr is the name I gave the script.

0
source

All Articles