Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gcc -E depth (preprocessing level)?

Say we have main.c:

#define f() { \
    foo();    \
    bar();    \
}

#define F() { \
    f();      \
    f();      \
}

F();

Now if we gcc -E main.c -o main.i, in main.i there is:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"
# 11 "main.c"
{ { foo(); bar(); }; { foo(); bar(); }; };

What if I don't want inner macros (like f()) to be expanded. How can I get something like:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"
# 11 "main.c"
{ f(); f(); };

Also if it's possible then how can I tune expansion depth?

like image 239
grabantot Avatar asked Mar 30 '26 00:03

grabantot


1 Answers

Simple answer (at least for the C preprocessors I know):

You cannot - Not in the general case. Macros are expanded by recursion and the recursion depth cannot be set with, for example, GNU cpp.

In specific cases where the "outer" and the "inner" macro are defined in different files you might be able to somewhat achieve what you want by simply not including (or providing an empty include of) the file that defines the "outer" definition and feeding the file with the "inner" definition directly into cpp.

I do, however, have a cpp implementation for an embedded system that actually does print every recursion step to stderr when executed with the highest verbosity ("-vvv"), although that doesn't help you with gcc/cpp - But this at least shows that something like you want is technically possible.

like image 50
tofro Avatar answered Apr 01 '26 14:04

tofro