Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all defined macros

Tags:

c

macros

I'm attempting to refactor a piece of legacy code and I'd like a snapshot of all of the macros defined at a certain point in the source. The code imports a ridiculous number of headers etc. and it's a bit tedious to track them down by hand.

Something like

#define FOO 1


int myFunc(...) {
    PRINT_ALL_DEFINED_THINGS(stderr)

    /* ... */
}

Expected somewhere in the output

MACRO: "FOO" value 1

I'm using gcc but have access to other compilers if they are easier to accomplish this task.

EDIT:

The linked question does not give me the correct output for this:

#include <stdio.h>

#define FOO 1

int main(void) {
    printf("%d\n", FOO);
}

#define FOO 0

This very clearly prints 1 when run, but gcc test.c -E -dM | grep FOO gives me 0

like image 401
Anthony Sottile Avatar asked Jun 24 '14 13:06

Anthony Sottile


1 Answers

To dump all defines you can run:

gcc -dM -E file.c

Check GCC dump preprocessor defines

All defines that it will dump will be the value defined (or last redefined), you won't be able to dump the define value in all those portions of code.

You can also append the option "-Wunused-macro" to warn when macros have been redefined.

like image 94
denisvm Avatar answered Oct 10 '22 07:10

denisvm