How to Detect If I'M Compiling Code With a Particular Visual Studio Version

How to Detect if I'm Compiling Code with a particular Visual Studio version?

_MSC_VER and possibly _MSC_FULL_VER is what you need. You can also examine visualc.hpp in any recent boost install for some usage examples.

Some values for the more recent versions of the compiler are:

MSVC++ 14.24 _MSC_VER == 1924 (Visual Studio 2019 version 16.4)
MSVC++ 14.23 _MSC_VER == 1923 (Visual Studio 2019 version 16.3)
MSVC++ 14.22 _MSC_VER == 1922 (Visual Studio 2019 version 16.2)
MSVC++ 14.21 _MSC_VER == 1921 (Visual Studio 2019 version 16.1)
MSVC++ 14.2 _MSC_VER == 1920 (Visual Studio 2019 version 16.0)
MSVC++ 14.16 _MSC_VER == 1916 (Visual Studio 2017 version 15.9)
MSVC++ 14.15 _MSC_VER == 1915 (Visual Studio 2017 version 15.8)
MSVC++ 14.14 _MSC_VER == 1914 (Visual Studio 2017 version 15.7)
MSVC++ 14.13 _MSC_VER == 1913 (Visual Studio 2017 version 15.6)
MSVC++ 14.12 _MSC_VER == 1912 (Visual Studio 2017 version 15.5)
MSVC++ 14.11 _MSC_VER == 1911 (Visual Studio 2017 version 15.3)
MSVC++ 14.1 _MSC_VER == 1910 (Visual Studio 2017 version 15.0)
MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015 version 14.0)
MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013 version 12.0)
MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012 version 11.0)
MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010 version 10.0)
MSVC++ 9.0 _MSC_FULL_VER == 150030729 (Visual Studio 2008, SP1)
MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008 version 9.0)
MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005 version 8.0)
MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003 version 7.1)
MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002 version 7.0)
MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0 version 6.0)
MSVC++ 5.0 _MSC_VER == 1100 (Visual Studio 97 version 5.0)

The version number above of course refers to the major version of your Visual studio you see in the about box, not to the year in the name. A thorough list can be found here. Starting recently, Visual Studio will start updating its ranges monotonically, meaning you should check ranges, rather than exact compiler values.

cl.exe /? will give a hint of the used version, e.g.:

c:\program files (x86)\microsoft visual studio 11.0\vc\bin>cl /?
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
.....

Is there a way to determine which version of Visual Studio was used to compile a static library?

For release libraries, it's unlikely that you could determine the version.

For debug libraries, you can use dumpbin:

dumpbin /rawdata:1 library.lib

The assembly manifest should be at the beginning of the dump and will contain the version of the CRT the library requires along with the full path to the compiler used to build the library.

For executables and DLLs you can get the linker version using dumpbin; it's under "OPTIONAL HEADER VALUES"

dumpbin /headers program.exe

Maybe someone else knows of a way to get the version for release libraries; I'm certainly interested too if they are.

Detecting compiler versions during compile time

As someone who has ported more than his fair share of 'C' around, I can see were you are coming from, so here is some to get us started:

For IBM CL/C++ compiler product:

__xlc__  - Format is V.R.M.F eg: "9.0.0.0"
__IBMCPP__ Format is an integer as VRM eg: V9.0 is 900
__IBMC__ - Format is an integer as VRM,
which indicates the level of compiler as VRM as:
< 200 is C Set/2
< 300 is C Set++
otherwise is Visual Age C++ V.M.C

where V=version, M=release, M=modification and F=fix level.

For Borland C:

___BORLANDC__ ??

For GNU C:

__GNUC__ ??

For Watcon C:

__WATCOMC__

Visual studio #if on visual studio version

What you have mentioned is called a #define, a compile time constant. These defines need to be.... defined (duh!!), if they are not defined then they are null therefore the expression fails. Your original linked example was for C++, so naturally it isn't defined for you in C#.

Under the hood Visual Studio uses MSBuild to do the actual compilation of your projects (MSBuild then invokes the compiler). The Visual Studio version you are using isn't available to MSBuild. Even trying to use a pre-build macro will not work because that is executed in the context of MSBuild/TFSBuild, at that point in time Visual Studio isn't within the context.

I should also point out that you have confused the Visual Studio version with the compiler version - and the compiler version also isn't natively available as a define.

Your best option is to create and use a custom MSBuild/TFSBuild build definition, you can then set these #defines as parameters to the build and you can also control which compiler is invoked (although that is too complex to show as an answer here). You would have one build definition for targeting the C# 6 compiler, and one for the default compiler.

Distinguish between VC++ 2010 and later compiler versions

#if (_MSC_VER == 1600)
//Visual C++ 2010 compiler code here...

Should do what you want. From here: How to Detect if I'm Compiling Code With Visual Studio 2008?

How to determine the version of the C++ standard used by the compiler?

By my knowledge there is no overall way to do this. If you look at the headers of cross platform/multiple compiler supporting libraries you'll always find a lot of defines that use compiler specific constructs to determine such things:

/*Define Microsoft Visual C++ .NET (32-bit) compiler */
#if (defined(_M_IX86) && defined(_MSC_VER) && (_MSC_VER >= 1300)
...
#endif

/*Define Borland 5.0 C++ (16-bit) compiler */
#if defined(__BORLANDC__) && !defined(__WIN32__)
...
#endif

You probably will have to do such defines yourself for all compilers you use.

How to check during setup if a certain program is running?

To do this during setup you will likely have to define a custom action. A custom action is an install step which allows you to run arbitrary code. It's too big of a topic to cover in a SO post, but the following article will help you get started on custom actions

  • http://msdn.microsoft.com/en-us/library/d9k65z2d(VS.80).aspx

In this action you could use the process class to determine if a particular process is running.

public static bool IsExcelRunning() {
return Process.GetProcesses().Where(x => x.ProcessName == "excel");
}

Can't remember off the top of my head if it's excel or msexcel but you can adjust appropriately ;).



Related Topics



Leave a reply



Submit