tags:

views:

137

answers:

4

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.

A: 

Definitely not from the library.

Your only chance to get this kind of information is before or during preprocessing; once the preprocessor is done, all information where macros came from is gone.

I don't know of any ways of doing this with the compiler alone; all I know that the -M family of GCC options gives you the dependencies of the source automatically.

You will have to resort to "third-party" tools, or come up with some find-and-grep logic yourself.

DevSolar
+7  A: 

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.

Kinopiko
this is the right answer
Shane C. Mason
The problem with the first part of the answer is that preprocessing the file removes all the `#define` directives and all the instances where `MACRONAME` was used (that's what the preprocessor does) so there's nothing left to grep for. But trick #2 is quite effective.
Michael Burr
You are right, the macro will be gone. I should have tested this first!
Kinopiko
+2  A: 

grep for the #define?

Marcin
The problem with this is what files to grep. If one used "makedepend" one could get a full list of dependencies, then feed this as the argument to grep.
Kinopiko
All files in your project. If it's not there, then you are redefining stuff you shouldn't be touching in the first place.
Marcin
Isn't that the problem though, tracking down something which was redefined which shouldn't have been?
Kinopiko
A: 
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.

Michael Dillon
not if it is being defined in system includes
Shane C. Mason
That doesn't really make sense, since you're searching through files which may not be included in the file.
Kinopiko