Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between cpp and gcc -E

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

like image 410
Bilow Avatar asked May 14 '17 18:05

Bilow


1 Answers

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

like image 158
l'L'l Avatar answered Oct 12 '22 20:10

l'L'l