Search code examples
c++creferencedebug-symbolslinker-flags

Find function/variable definition for a reference in source code


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.


Solution

  • 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: