Finding version of Microsoft C++ compiler from command-line (for makefiles)

后端 未结 6 1167
孤城傲影
孤城傲影 2020-12-04 21:00

I must be missing something really obvious, but for some reason, the command-line version of the Microsoft C++ compiler (cl.exe) does not seem to support reporting just its

6条回答
  •  日久生厌
    2020-12-04 21:46

    I had the same problem today. I needed to set a flag in a nmake Makefile if the cl compiler version is 15. Here is the hack I came up with:

    !IF ([cl /? 2>&1 | findstr /C:"Version 15" > nul] == 0)
    FLAG = "cl version 15"
    !ENDIF
    

    Note that cl /? prints the version information to the standard error stream and the help text to the standard output. To be able to check the version with the findstr command one must first redirect stderr to stdout using 2>&1.

    The above idea can be used to write a Windows batch file that checks if the cl compiler version is <= a given number. Here is the code of cl_version_LE.bat:

    @echo off
    FOR /L %%G IN (10,1,%1) DO cl /? 2>&1 | findstr /C:"Version %%G" > nul && goto FOUND
    EXIT /B 0
    :FOUND
    EXIT /B 1
    

    Now if you want to set a flag in your nmake Makefile if the cl version <= 15, you can use:

    !IF [cl_version_LE.bat 15]
    FLAG = "cl version <= 15"
    !ENDIF
    

提交回复
热议问题