[visual-c++] Finding version of Microsoft C++ compiler from command-line (for makefiles)

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 version when run. We need this to write makefiles that check the compiler version a user of our tool has installed (they get makefiles with code they are to compile themselves locally, so we have no control over their compiler version).

In gcc, you just give the option -v or --version to get a nice version string printed.

In cl.exe, you get an error for -v.

I have read the MSDN docs and compiler online help, and I cannot find the switch to just print the compiler version. Annoyingly, you always get the version when the compiler starts... but you seem not to be able to start the compiler just to get the version out of it.

Finding compiler vendor / version using qmake seemed similar, but only deals with the simple case of gcc.

I am trying this with VC++ Express 2005, if that matters. I hoped it would not, as detecting the compiler version is best done in a compiler-version-independent way :)

Update, after replies:

  • Running cl.exe without any arguments prints its version and some help text.
  • This looks like the most portable way to get at the version, across vc versions.
  • You then have to parse a multi-line output, but that is not too difficult.
  • We did this in the end, and it works.

This question is related to visual-c++ makefile

The answer is


Just run it without options.

P:\>cl.exe
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

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

Try:

cl /v

Actually, any time I give cl an argument, it prints out the version number on the first line.

You could just feed it a garbage argument and then parse the first line of the output, which contains the verison number.


Create a .c file containing just the line:

_MSC_VER

or

CompilerVersion=_MSC_VER

then pre-process with

cl /nologo /EP <filename>.c

It is easy to parse the output.


Have a look at C++11 Features (Modern C++)

and section "Quick Reference Guide to Visual C++ Version Numbers" ...