How to Find Out Cl.Exe's Built-In MACros

How to find out cl.exe's built-in macros

This method does amount to asking the compiler for the list of predefined macros, but it uses undocumented features and provides only a partial list. I include it here for completeness.

The Microsoft C/C++ compiler allows an alternative compiler front-end to be invoked using the /B1 and /Bx command line switches for .c and .cpp files respectively. The command-line interface module CL.exe passes a list of options to the replacement compiler front-end via the MSC_CMD_FLAGS environment variable. This list of options includes -D macro definitions for some of the predefined macros.

The following trivial replacement compiler front-end prints out the list of options passed to it:

/* MyC1.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p;

if ((p = getenv("MSC_CMD_FLAGS")) != NULL)
printf("MSC_CMD_FLAGS:\n%s\n", p);

if ((p = getenv("MSC_IDE_FLAGS")) != NULL)
printf("MSC_IDE_FLAGS:\n%s\n", p);

return EXIT_FAILURE;
}

Compile this to an executable named, for example, "MyC1.exe", ensure it is visible in the PATH and tell CL.exe to invoke it as the compiler front-end using one of the following:

cl /B1MyC1.exe AnyNameHere.c  
cl /BxMyC1.exe AnyNameHere.cpp

Include other command-line options as required to see which macros are predefined for that set of options.

In the resulting output look for the -D options. An example list is given below. In the actual output the list will be space-separated, with each macro definition preceded by -D, and other options also present.

_MSC_EXTENSIONS  
_INTEGRAL_MAX_BITS=64
_MSC_VER=1600
_MSC_FULL_VER=160030319
_MSC_BUILD=1
_WIN32
_M_IX86=600
_M_IX86_FP=0
_MT

This technique seems to include most macros that depend on command-line options, but excludes those that are always defined such as __FILE__ and __DATE__.

Is there any way I can tell if my code is being compiled with cl.exe?

If you want to make decisions on the cl.exe version, use _MSC_BUILD.

However, first check for _MSC_VER, because _MSC_BUILD was not available in VS 2005.

Microsoft C++ Pre-defined Macros

Unfortunately, I don't think MSVC has a built in way of doing this.

I've used the following program to dump values of 'known' predefined symbols. I should give attribution (because I know I didn't come up with this), but I don't have notes on where I got it from (update: looks like I probably got it from here: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/644c12ed-e3a7-4c5a-a73a-610fcc7913ca)...

#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __PPOUT__(x) "#define " #x " " __STR1__(x)

#if defined(_ATL_VER)
#pragma message(__PPOUT__(_ATL_VER ))
#endif

#if defined(_CHAR_UNSIGNED )
#pragma message(__PPOUT__(_CHAR_UNSIGNED ))
#endif

#if defined(__CLR_VER )
#pragma message(__PPOUT__(__CLR_VER ))
#endif

#if defined(__cplusplus_cli )
#pragma message(__PPOUT__(__cplusplus_cli ))
#endif

#if defined(__COUNTER__ )
#pragma message(__PPOUT__(__COUNTER__ ))
#endif

#if defined(__cplusplus )
#pragma message(__PPOUT__(__cplusplus ))
#endif

#if defined(_CPPLIB_VER )
#pragma message(__PPOUT__(_CPPLIB_VER ))
#endif

#if defined(_CPPRTTI )
#pragma message(__PPOUT__(_CPPRTTI ))
#endif

#if defined(_CPPUNWIND )
#pragma message(__PPOUT__(_CPPUNWIND ))
#endif

#if defined(_DEBUG )
#pragma message(__PPOUT__(_DEBUG ))
#endif

#if defined(_DLL )
#pragma message(__PPOUT__(_DLL ))
#endif

#if defined(__FUNCDNAME__ )
#pragma message(__PPOUT__(__FUNCDNAME__ ))
#endif

#if defined(__FUNCSIG__ )
#pragma message(__PPOUT__(__FUNCSIG__ ))
#endif

#if defined(__FUNCTION__ )
#pragma message(__PPOUT__(__FUNCTION__ ))
#endif

#if defined(_INTEGRAL_MAX_BITS )
#pragma message(__PPOUT__(_INTEGRAL_MAX_BITS ))
#endif

#if defined(_M_ALPHA )
#pragma message(__PPOUT__(_M_ALPHA ))
#endif

#if defined(_M_CEE )
#pragma message(__PPOUT__(_M_CEE ))
#endif

#if defined(_M_CEE_PURE )
#pragma message(__PPOUT__(_M_CEE_PURE ))
#endif

#if defined(_M_CEE_SAFE )
#pragma message(__PPOUT__(_M_CEE_SAFE ))
#endif

#if defined(_M_IX86 )
#pragma message(__PPOUT__(_M_IX86 ))
#endif

#if defined(_M_IA64 )
#pragma message(__PPOUT__(_M_IA64 ))
#endif

#if defined(_M_IX86_FP )
#pragma message(__PPOUT__(_M_IX86_FP ))
#endif

#if defined(_M_MPPC )
#pragma message(__PPOUT__(_M_MPPC ))
#endif

#if defined(_M_MRX000 )
#pragma message(__PPOUT__(_M_MRX000 ))
#endif

#if defined(_M_PPC )
#pragma message(__PPOUT__(_M_PPC ))
#endif

#if defined(_M_X64 )
#pragma message(__PPOUT__(_M_X64 ))
#endif

#if defined(_MANAGED )
#pragma message(__PPOUT__(_MANAGED ))
#endif

#if defined(_MFC_VER )
#pragma message(__PPOUT__(_MFC_VER ))
#endif

#if defined(_MSC_BUILD )
#pragma message(__PPOUT__(_MSC_BUILD ))
#endif

#if defined(_MSC_EXTENSIONS )
#pragma message(__PPOUT__(_MSC_EXTENSIONS ))
#endif

#if defined(_MSC_FULL_VER )
#pragma message(__PPOUT__(_MSC_FULL_VER ))
#endif

#if defined(_MSC_VER )
#pragma message(__PPOUT__(_MSC_VER ))
#endif

#if defined(__MSVC_RUNTIME_CHECKS )
#pragma message(__PPOUT__(__MSVC_RUNTIME_CHECKS ))
#endif

#if defined(_MT )
#pragma message(__PPOUT__(_MT ))
#endif

#if defined(_NATIVE_WCHAR_T_DEFINED)
#pragma message(__PPOUT__(_NATIVE_WCHAR_T_DEFINED))
#endif

#if defined(_OPENMP )
#pragma message(__PPOUT__(_OPENMP ))
#endif

#if defined(_VC_NODEFAULTLIB )
#pragma message(__PPOUT__(_VC_NODEFAULTLIB ))
#endif

#if defined(_WCHAR_T_DEFINED )
#pragma message(__PPOUT__(_WCHAR_T_DEFINED ))
#endif

#if defined(_WIN32 )
#pragma message(__PPOUT__(_WIN32 ))
#endif

#if defined(_WIN64 )
#pragma message(__PPOUT__(_WIN64 ))
#endif

#if defined(_Wp64 )
#pragma message(__PPOUT__(_Wp64 ))
#endif

void main() {}

How to figure out what value MSVC is using for a preprocessor macro

Try one of the following options to CL.exe:

/E preprocess to stdout
/P preprocess to file

If you're building within Visual Studio, you can specify custom command-line options in one of the project property dialogs.

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.30 _MSC_VER == 1933 (Visual Studio 2022 version 17.3.4)
MSVC++ 14.30 _MSC_VER == 1932 (Visual Studio 2022 version 17.2.2)
MSVC++ 14.30 _MSC_VER == 1930 (Visual Studio 2022 version 17.0.2)
MSVC++ 14.30 _MSC_VER == 1930 (Visual Studio 2022 version 17.0.1)
MSVC++ 14.28 _MSC_VER == 1929 (Visual Studio 2019 version 16.11.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.9.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.8.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.8.1)
MSVC++ 14.27 _MSC_VER == 1927 (Visual Studio 2019 version 16.7)
MSVC++ 14.26 _MSC_VER == 1926 (Visual Studio 2019 version 16.6.2)
MSVC++ 14.25 _MSC_VER == 1925 (Visual Studio 2019 version 16.5.1)
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
.....

How do I see a C/C++ source file after preprocessing in Visual Studio?

cl.exe, the command line interface to Microsoft Visual C++, has three different options for outputting the preprocessed file (hence the inconsistency in the previous responses about Visual C++):

  • /E: preprocess to stdout (similar to GCC's -E option)
  • /P: preprocess to file
  • /EP: preprocess to stdout without #line directives

If you want to preprocess to a file without #line directives, combine the /P and /EP options.

How to see a list of SPSS macros presently defined

To get a list of already defined Macros use the following Syntax:

DISPLAY MACROS.

To my knowledge there is no way to get the macro definitions directly (unless you read the syntax files where they are defined).



Related Topics



Leave a reply



Submit