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.

16
  • grep -rw myfunctioname $HOME/src/myproject/*? Commented Apr 9, 2014 at 18:43
  • this is source code searching: I was hoping for something better. It takes a lot of time Commented Apr 9, 2014 at 18:44
  • 1
    In case you use an IDE, this might provide features to do so. Eclipse (eclipse.org) for example does. Commented Apr 9, 2014 at 18:45
  • "... it takes a lot of time" how many lines of code are we talking about? Commented Apr 9, 2014 at 18:46
  • 1
    cscope is another reasonable alternative Commented Apr 9, 2014 at 20:34

2 Answers 2

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:
Sign up to request clarification or add additional context in comments.

2 Comments

How can I exit from this tool?
You can use Ctrl-d to exit from cscope
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.