67

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.
Community
  • 1
  • 1
jakobengblom2
  • 5,531
  • 2
  • 25
  • 33
  • I find [Microsoft C++ language and standard library conformance table](https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-160) – yu yang Jian May 09 '21 at 13:08

7 Answers7

49

Are you sure you can't just run cl.exe without any input for it to report its version?

I've just tested running cl.exe in the command prompt for VS 2008, 2005, and .NET 2003 and they all reported its version.

For 2008:

d:\Program Files\Microsoft Visual Studio 9.0\VC>cl

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86

For 2005, SP 1 (added Safe Standard C++ classes):

C:\Program Files\Microsoft Visual Studio 8\VC>cl

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86

For 2005:

C:\Program Files\Microsoft Visual Studio 8\VC>cl

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86

For .NET 2003:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.6030 for 80x86

EDIT

For 2010, it will be along the line of:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.XX.YYYYY.ZZ for 80x86

or depending on targeted platform

Microsoft (R) C/C++ Optimizing Compiler Version 16.XX.YYYYY.ZZ for x64

For 2012:

Microsoft (R) C/C++ Optimizing Compiler Version 17.XX.YYYYY.ZZ for $$$

where $$$ is the targeted platform (e.g. x86, x64, ARM), and XX, YYYYY, and ZZ are minor version numbers.

For 2013:

Microsoft (R) C/C++ Optimizing Compiler Version 18.XX.YYYYY.ZZ for $$$

where $$$ is the targeted platform (e.g. x86, x64, ARM), and XX, YYYYY, and ZZ are minor version numbers.

For 2015:

Microsoft (R) C/C++ Optimizing Compiler Version 19.XX.YYYYY for $$$

where $$$ is the targeted platform (e.g. x86, x64, ARM), and XX and YYYYY are minor version numbers.

Community
  • 1
  • 1
KTC
  • 8,967
  • 5
  • 33
  • 38
  • 1
    Yes, that kind of works. But it also gives a few additional lines of output. Guess we just need to parse harder than we have to for gcc. – jakobengblom2 Aug 05 '09 at 13:57
  • 3
    This is correct you can just call `cl` and you will have the version there available to be parsed, just take into account that it will change with the VS language, for example for VS 2010 in spanish you will get `Compilador de optimización de C/C++ de Microsoft (R) versión 16.00.30319.01 para x64`, I just found a important project (https://jdk8.java.net/) relying in english version for getting the version through parsing and this just is not right, because it won't work in my spanish Visual Studio version – Jaime Hablutzel Apr 12 '14 at 03:29
  • 1
    @TripleS there you go. – KTC Apr 26 '14 at 19:43
  • VS2017 allegedly added ARMv8 (ARM64). But I [can't figure out how to engage it](http://stackoverflow.com/q/41688101/608639). I have ARMv8 code ready to enable for MS IoT gadgets... Microsoft's lack of documentation is very frustrating. – jww Mar 18 '17 at 21:30
  • 1
    For VS2017, cl.exe is (probably) in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.XX.XXXXX\bin\Hostx64\x64 (note the X's, this varies depending on your MSVC++ compiler version). – llf Jul 14 '18 at 07:34
12

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
Linoliumz
  • 2,381
  • 3
  • 28
  • 35
7

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.

Rexo
  • 71
  • 1
  • 1
4

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... ]
Mark
  • 106,305
  • 20
  • 172
  • 230
3

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64>cl

Dimitrios Ververidis
  • 1,118
  • 1
  • 9
  • 33
1

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

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

  • How does one use *"Visual C++ Version Numbers"* from within `nmake`? *"`_MSC_VER` version number"* is fine for source files, but its not clear how its useful for makefiles. *"Branded version number"* and *"Internal version number"* seem even less useful. – jww Sep 07 '16 at 23:10
-6

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.

samoz
  • 56,849
  • 55
  • 141
  • 195
  • 3
    that is downright dangerous, as /v is deprecated and supposed to add a version number to the generated binary. It is not what I am looking for. – jakobengblom2 Aug 05 '09 at 13:57