2

My project contains many files.

Sometimes I need to know where a particular function is defined (implemented) in source code. What I currently do is text search within source files for the function name, which is very time consuming.

My question is: Is there a better way (compiler/linker flag) to find that function definition in source files?.... Since the linker has gone through all the trouble of resolving all these references already.

I am hoping for method better than stepping into a function call in debugger, since a function can be buried within many calls.

aiao
  • 4,621
  • 3
  • 25
  • 47
  • `grep -rw myfunctioname $HOME/src/myproject/*`? – alk Apr 09 '14 at 18:43
  • this is source code searching: I was hoping for something better. It takes a lot of time – aiao Apr 09 '14 at 18:44
  • 1
    In case you use an IDE, this might provide features to do so. Eclipse (http://eclipse.org/) for example does. – alk Apr 09 '14 at 18:45
  • "*... it takes a lot of time*" how many lines of code are we talking about? – alk Apr 09 '14 at 18:46
  • @alk I would guess about a 200k – aiao Apr 09 '14 at 18:50
  • Depends on your IDE, for Visual Studio + Visual Assist X it is quite simple – Cory Kramer Apr 09 '14 at 18:51
  • @alk I was thinking I have missed something, and there is something like debug symbols and a plugin in text editor to reference these files. – aiao Apr 09 '14 at 18:53
  • ctags predates all of those ide's. most of them based their tag system directly off of ctags. use ctags. – Steve Cox Apr 09 '14 at 19:03
  • So which tools do you actually use? – alk Apr 09 '14 at 19:07
  • @SteveCox Sticks and stones predate jet aircraft, but I would not suggest using the former in a war. Older =/= better. Intellisense is possibly the most beautiful tool that I've seen that does this (although the rest of Visual Studio is... to be left out of this comment). – Suedocode Apr 09 '14 at 19:08
  • ctags is the only separate tool that does this. just run ctags from the command line then use vim/emacs/sublimetext (notepad++ probably has an extension too) to explore the tags – Steve Cox Apr 09 '14 at 19:09
  • http://www.codeproject.com/Articles/679180/Exuberant-Ctags-and-Notepadplus – Steve Cox Apr 09 '14 at 19:11
  • @SteveCox ctags Is the sort of thing I am looking for (I think, need to read more). I used to work with code:blocks but it would sometimes refer me to wrong function definition or "function not found". What would be perfect is to use the compiler parser as a second parser may (and have produced) different results. But ctags I can probably live with – aiao Apr 09 '14 at 19:15
  • @aiao if you write code with this sort of lookup in mind, this works really well. unfortunately if the 200k line base you're working with didn't do this for you it can be hard. there is a trick we use though with the -M option on some compilers to dump all of the dependancies in a folder, and then generate the ctags just for that folder. it bloats your total tag files across a project, but it can make tag lookup way more specific if the source is divided between a lot of folders, and people haven't been too promiscuous with header dependancies ;) – Steve Cox Apr 09 '14 at 19:23
  • For 200k++ lines I'd definetly use Eclipse. – alk Apr 09 '14 at 19:27
  • 1
    `cscope` is another reasonable alternative – twalberg Apr 09 '14 at 20:34

2 Answers2

2

Try cscope utility.

From the manual:

Allows searching code for:

  • all references to a symbol
  • global definitions
  • functions called by a function
  • functions calling a function
  • text string
  • regular expression pattern
  • a file
  • files including a file

  • Curses based (text screen)

  • An information database is generated for faster searches and later reference
  • The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)
  • Has a command line mode for inclusion in scripts or as a backend to a GUI/frontend
  • Runs on all flavors of Unix, plus most monopoly-controlled operating systems.

A "screenshot":

C symbol: atoi

  File     Function     Line
  0 stdlib.h <global>      86 extern int atoi (const char *nptr);
  1 dir.c    makefilelist 336 dispcomponents = atoi(s);
  2 invlib.c invdump      793 j = atoi(term + 1);
  3 invlib.c invdump      804 j = atoi(term + 1);
  4 main.c   main         287 dispcomponents = atoi(s);
  5 main.c   main         500 dispcomponents = atoi(s);
  6 stdlib.h atoi         309 int atoi (const char *nptr) __THROW



  Find this C symbol:
  Find this global definition:
  Find functions called by this function:
  Find functions calling this function:
  Find this text string:
  Change this text string:
  Find this egrep pattern:
  Find this file:
  Find files #including this file:
Zinovy Nis
  • 455
  • 6
  • 9
0

If the symbol is exported, then you could wire up objdump or nm and look at the .o files. This is not useful for finding things in header files though.

My suggestion would be to put your project in git (which carries numerous other advantages) and use git grep which looks only at those files under git's revision control (meaning you don't grep object files and other irrelevances). git grep is also nice and quick.

abligh
  • 24,573
  • 4
  • 47
  • 84