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

2019-01-06 17:57发布

Does anyone know how could I find out which are cl.exe's builtin/predefined macros? For example for gcc the following command line will list all the compiler's builtin macros

gcc -dM -E - </dev/null

EDIT: I'm interested in a way similar to gcc's that is "ask the actual compiler".

Thanks

4条回答
萌系小妹纸
2楼-- · 2019-01-06 18:29

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

查看更多
爷、活的狠高调
3楼-- · 2019-01-06 18:36

Try the predef project. They maintain a database of predefined macros for many target platforms, host platforms and compiler toolchains.

They also have a script that attempts to discover all of the predefined names whether documented or not. It works by running the strings utility over the compiler, processing that to get plausible candidate tokens, and trying test compilations for each token. Not fast, but pretty good at discovering lots of macros.

查看更多
女痞
4楼-- · 2019-01-06 18:43
趁早两清
5楼-- · 2019-01-06 18:48

/P preprocessor flag will emit the currently active macros based on the project build settings. I am not sure if it is exactly the equivalent of gcc command you have shown. The output is in .I file.

查看更多
登录 后发表回答