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

7

27

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

Bead answered 8/9, 2010 at 7:36 Comment(0)
B
28

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

Brumbaugh answered 8/9, 2010 at 22:19 Comment(2)
This is close than anything else so +1 from me and accepted. I managed to find most of the predefined macros using also the strings utility technet.microsoft.com/en-us/sysinternals/bb897439.aspxBead
There is now a /PD flag built in- see the newer answers below.Automate
S
5

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

Somaliland answered 8/9, 2010 at 8:9 Comment(2)
Not quite the same thing as gcc does not preprocess anything with that command.Bead
@Bead GCC absolutely does preprocess (note that -E is included), e.g. you can pass <<< '#include <limits.h>' to include macros from a given header.Pulmonic
A
3

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.

Acupuncture answered 8/9, 2010 at 9:23 Comment(0)
M
3

I don't know in what version this was made available.

/PD print all macro definitions

Example use:

  • Create an empty file foo.cpp (e.g. echo // > foo.cpp)
  • cl /Zc:preprocessor /PD foo.cpp
Meares answered 12/7, 2021 at 22:50 Comment(0)
H
3

The available pre-defined C/C++ macros in MSVC, when using cl for compilation, depends (of course) on what you are compiling (C vs C++) and the variants for that, including architecture and other cl options. (Check your options with cl /help for additional info.)

Then the powershell (one-liner) commands are:

# For C++
echo // > foo.cpp; cl /nologo /Zc:preprocessor /PD /EHs /TP foo.cpp |sort; rm foo.cpp, foo.obj

# For C
echo // > foo.cpp; cl /nologo /Zc:preprocessor /PD /EHs /TC foo.cpp |sort; rm foo.cpp, foo.obj

For example, the output for C is:

#define __STDC_HOSTED__ 1
#define _INTEGRAL_MAX_BITS 64
#define _IS_ASSIGNABLE_NOCHECK_SUPPORTED 1
#define _M_AMD64 100
#define _M_X64 100
#define _MSC_BUILD 0
#define _MSC_EXTENSIONS 1
#define _MSC_FULL_VER 192930147
#define _MSC_VER 1929
#define _MSVC_EXECUTION_CHARACTER_SET 1252
#define _MSVC_TRADITIONAL 0
#define _MSVC_WARNING_LEVEL 1L
#define _MT 1
#define _WIN32 1
#define _WIN64 1
foo.cpp
LINK : fatal error LNK1561: entry point must be defined

If you wanna do the same trickery for using gcc in powershell, you need to use something like this:

echo `EOF` | gcc -E -dM -xc++ - |sort
Hairdresser answered 27/11, 2022 at 17:37 Comment(1)
A one-liner for cmd: pushd %tmp% && echo // > x.cpp && cl /nologo /EP /Zc:preprocessor /PD x.cpp && del x.cpp x.obj && popdRostellum
L
2

Get information at the source!

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

Lylalyle answered 8/9, 2010 at 7:38 Comment(3)
I found that page(I usually don't post a question until exhausting any other resources :) ) but I'm interested in a way to ask the actual compiler for that list of macros.Bead
Note that the page is rather incomplete. If you compare it with the list at predef project (from the other answer), it for example completely omits some architectures.Dinin
_M_ARM is missing from the list for Windows Phone and Windows RT projects (even under the Visual Studio 2013 version of the docs).Inflect
B
2

As of Visual C++ 16.8.0, there is such an option. See https://developercommunity.visualstudio.com/t/provide-the-ability-to-list-predefined-macros-and/934925. Pass /EP /Zc:preprocessor /PD to the compiler along with an empty (or non-empty) source file. The option requires use of the "new" preprocessor.

Braw answered 17/11, 2022 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.