9

I thought that both cpp foo.c and gcc -E foo.c do preprocess the source file the same way, but I got their output to differ for the same file.

$ cat foo.c
#define VARIABLE 3
#define PASTER(x,y) x ## _ ## y
#define EVALUATOR(x,y)  PASTER(x,y)
#define NAME(fun) EVALUATOR(fun, VARIABLE)

extern void NAME(mine);

Result for cpp:

$ cpp foo.c
# 1 "foo.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 329 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "foo.c" 2





extern void mine ## _ ## 3;

$

Result for gcc -E and for clang -E:

$ gcc -E foo.c
# 1 "foo.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 330 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "foo.c" 2





extern void mine_3;
$

Why do those outputs differ, and which one should I use when I want to see the preprocessed source ?

Original code here

Community
  • 1
  • 1
Bilow
  • 2,194
  • 1
  • 19
  • 34
  • I just tried this. My version of `cpp` produces identical output as `gcc -E`. Same as your `gcc` output. – selbie May 14 '17 at 18:37
  • 2
    I get the same results with `cpp foo.c` as with `gcc -E foo.c`. Can you paste the output of `cpp -v foo.c` into the question? – Michael Burr May 14 '17 at 18:38
  • 1
    If I use `--traditional-cpp` as a command line parameter to cpp, i get the same output as yours. Looks likes someone already identified that as a likely answer. – selbie May 14 '17 at 18:41
  • The first line of `cpp -v foo.c` is `Apple LLVM version 8.0.0 (clang-800.0.42.1)`. Rest of output is quite verbose – Bilow May 14 '17 at 18:55

1 Answers1

4

The difference between the two is that gcc -E will eliminate -traditional-cpp. If you include the option then you should receive the same result as cpp.

https://gcc.gnu.org/onlinedocs/cpp/Traditional-Mode.html

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Indeed. How does gcc manage to eliminate this option as there is no `-no-traditional-cpp` ? – Bilow May 14 '17 at 18:50
  • 1
    Looks like some `cpp` programs (like the one on my workstation) are built to not default to `-traditional-cpp`. So if you want to turn off traditional mode when invoking `cpp`, any idea how to do it? My version complains if I try to use `-no-traditional-cpp` or `--no-traditional-cpp`. This is just academic for me since invoking `gcc -E` instead does the trick. – Michael Burr May 14 '17 at 18:51
  • @Bilow: `gcc` typically eliminates it unless specified otherwise. – l'L'l May 14 '17 at 18:52
  • 1
    I will use `gcc -E` but I'm still wondering how gcc does eliminate `-traditional-cpp` since I can't do it in command line – Bilow May 14 '17 at 18:57
  • 1
    Thank you all for the answers – Bilow May 14 '17 at 19:10