13

I'd like to see all macros that are defined by the invocation of the compiler I'm using. Is there any way to do this? I have seen in the manual it says you can use cpp -dM but this doesn't work for me. Perhaps I'm doing something wrong?

When I run:

cpp -dM

I get no output at all from the preprocessor. If I try adding -dM as an option on gcc, I don't notice any difference.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Brandon Yates
  • 2,022
  • 3
  • 22
  • 33
  • "Doesn't work for me" - please be more descriptive! – Oliver Charlesworth Jun 05 '12 at 21:01
  • 3
    On my Linux computer, the output of `cpp -dM < /dev/null | wc -l` is `124`, so there are 124 predefined values. `cpp -dM < /dev/null | less` shows me what they are. What is the output of those commands on your computer? – Robᵩ Jun 05 '12 at 21:03
  • Sorry I tried to clear it up a little – Brandon Yates Jun 05 '12 at 21:03
  • Rob thank you I'll have to try and figure out how to do that from windows, which unfortunately I am stuck using. I'm using code sourcery cross compiler for arm. (A gcc port essentially) – Brandon Yates Jun 05 '12 at 21:04

2 Answers2

18

You can use:

gcc -dM -E - < /dev/null

Note that you can also get the compiler macros in addition with this command:

touch bla.c && gcc -dM -E bla.c

For example on my computer:

$ touch bla.c && gcc -dM -E bla.c | wc -l
486
$ gcc -dM -E - < /dev/null | wc -l
124
$
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 3
    also useful are `-xc`, `-xc++` and `-std=...` so you can compare language and dialect-specific definitions – Christoph Jun 05 '12 at 21:18
  • 1
    @Christoph actually I tried first `gcc -xc -dM -E - < /dev/null` for my second example, but this does not seem to work with `-dM` (same result as in the first example) – ouah Jun 05 '12 at 21:21
  • @Christoph ok so it has been fixed because it doesn't on my gcc-4.4.3/Linux – ouah Jun 05 '12 at 21:30
5

By default, cpp -dM will read its input file from standard input and write to standard output. Since you're not trying to preprocess any input, you can pass it the empty input using /dev/null:

# Option 1
cpp -dM < /dev/null
# Optio n2
cpp -dM /dev/null

On Windows, you can use the NUL pseudofile instead of /dev/null.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589