Track preprocessor's replacements

Viewed 43

In my C++ project, I have a header with a line like this:

enum { OK, ERROR_1, ERROR_2 };

When compiling with GCC (v 9.4.0), I get

error: expected identifier before '(' token

Examining the preprocessor output gives

enum { 
# 53 "/path/to/file.h" 3 4
     (0)
# 53 "/path/to/file.h"
        , ERROR_1, ERROR_2 };

I searched my project for a macro that would define OK and replace it with (0) but to no avail. So my question is how can I track where this (0) comes from? I read the docs on preprocessor output, but haven't found anything that would aid me in my problem.

1 Answers

You can use for example -E -fdirectives-only as options to GCC. It will give you a preprocessor output with all #includes resolved and including file/line markers, but with the macro definitions still in place and unexpanded.

Then simply search for #define OK in the output and search upwards for a # N marker where N is an integer. The marker will refer to the file/line from where the definition originates.

(By the way, you are looking on the wrong page of documentation. For the possible command line options affecting the preprocessor see https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html.)

Related