Question

I am using some macro in my source file (*.c ) .

Is there any way during compilation or from the library that I can identify the exact header file from which this particular macro is getting resolved ?

The issue is we are using a macro #defined to 10 in some header file, but the value being received in the code is 4 . So instead of going and checking in all the dep files , we want to know incase there is some direct way to identify the source from which the macro got resolved.

Was it helpful?

Solution

If you just run cpp (the C preprocessor) on the file, the output will contain #line directives of the form

#line 45 "silly-file-with-macros.h"

for the compiler saying where everything came from. So one way is to use

 cpp my-file.c | more

and look for the #line directive.

Depending on your compiler, another trick you could use is to redefine the macro to something else, and the compiler will spit out a warning like

test-eof.c:5:1: warning: "FRED" redefined
test-eof.c:3:1: warning: this is the location of the previous definition

(this is from gcc) which should tell you where the macro was previously defined. But come to think of it, how is it that you aren't getting that warning already?

Another idea is to use makedepend to get a list of all the included files, then grep them for #define lines in them.

OTHER TIPS

grep for the #define?

find / -name '*.h' | xargs -L 100 grep -H macroname

There are three commands there. The find command selects which files to be searched so you could change that to '.c' or '.cpp' or whatever you need. Then the xargs command, splits the list of files into 100 at a time so that you don't overflow some internal shell command buffer size. Then the grep command is run repeatedly with each list of 100 files and it prints any filenames containing macroname and the line of code that uses it.

From this you should be able to see where it is being redefined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top