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

不想你离开。 提交于 2019-11-26 16:24:49

问题


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


回答1:


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




回答2:


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




回答3:


Get information at the source!

http://msdn.microsoft.com/en-us/library/b0084kay%28v=VS.90%29.aspx




回答4:


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.



来源:https://stackoverflow.com/questions/3665537/how-to-find-out-cl-exes-built-in-macros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!