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__.

Leave a Comment