finding where a symbol is #define'd

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there any type of cross-reference listing or search, either in gcc or in an external tool, to find where a symbol is #define'd in some .h file?

I'm trying to get started with Contiki, which has a large tree-structure of files, and many 'nested' .h files. So, when I see a name in a .c file, and that name was defined elsewhere in some .h file, it's very difficult to find where that name is defined.

Yes, I can grep, and find many files where it is defined, but it's then takes a fair amount of time and effort to figure out which of those were actually involved in the compilation of source code in question.

If these were symbols/names in .c files, I presume I could get a linker cross-ref, but since the #defines are in .h files, it seems this info is essentially hidden by the preprocessing.

Is there a way to find this?

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You may want to try out source navigator.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What if you try to redefine the symbol that you are interested in? The compiler then should complain about the redefinition and tell you where the original definition is.

Eugene

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BobSRQ wrote:
Is there any type of cross-reference listing or search, either in gcc or in an external tool, to find where a symbol is #define'd in some .h file?
Any editor that claims to be a programmer's editor should have the ability to search for a series of characters (using regex, "word only" and similar qualifications) across a set of files either currently loaded or in a directory tree. If yours doesn't, perhaps it's time to find a new one.

An alternative, is to use grep/fgrep, sed or other Unix-like tools to find identifiers in a directory tree.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think I've stumbled across this in "basic editors", but Geany(geany.org) might. I haven't tried it out in action myself, but I've seen it has gained some traction.

AVR Studio 5 supports it, so if you're running Windows you might want to try to get Contiki imported and setup there.

11011110101011011100000011011110

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bob,

Are you using Windows or Linux? If Windows and you are serious about this consider spending $239 on this: http://www.sourceinsight.com/buy...

Of all the source browsing editors I've ever used I consider this the best. One nice feature with #define's is that you can right-click any one and set its defined state to 0 or 1 then the editor will grey sections of code that will not be built because of the non-defined state of that define.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson, if you have used Sourcenavigator, how would it compare to Sourceinsight?

To the original question, I would have suggested NotePad++, but that Geany looks fine too, I have to try that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I'm on Linux I use SourceNavigatorNG. On Windows I use SourceInsight. It's like chalk and cheese. In fact for serious work on the Linux kernel I normally share the source tree to Windows so I can use SourceInsight to edit it.

I have to admit that for a lot of "quick edits" I actually use NotePad++ on Windows and nano on Linux.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can add the gcc-options "-g3 -save-temps" and look for the i-file (C) or ii-file (C++) gcc produces.

For every Macro is displays to what it resolves and what includes come from where. Moreover this shows also if a define is from command line (e.g. -Dfoo=x) or a built-in define like __VERSION__.

It's not always feasible or convenient or appropriate to import a project into bulky IDE just to see where a macro comes from...

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you are only interested in one platform you can delete all the other platform and cpu folders. That would simplify the search considerably. You often have to recursively grep because the define came from another define, and the strings are munged in such a way that they don't both come up on the same search. E.g.
#define uip_getdraddr(addr) uip_ipaddr_copy((addr), &uip_draddr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
find . -name \*.h -exec grep -H -n $SEARCH_TERM {} \;

works for me under cygwin (also linux of course).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But to find where the symbol is actually #define'd then $SEARCH_TERM
should be something like:
"#define.*SYMBOLNAME"

(type it with the quotes)

--- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But to find where the symbol is actually #define'd

Not usually worth it - if the find is limited to .h then it's rare for there to be more than a few occurrences and the human eye/brain is pretty good at picking out the one starting "#define "

(of course this won't catch -D's, may be worth exploring the Makefile for those).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SprinterSB wrote:
You can add the gcc-options "-g3 -save-temps" and look for the i-file (C) or ii-file (C++) gcc produces.
-dD is more to the point - or, maybe even better, running directly cpp with -dM or -dD or similar.

Consult the manual.

JW