Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a function definition using a call in C++

I'm trying to understand the source code of a fairly large C++ project. The source wasn't written with an IDE, so I don't have a "goto" button to go straight to the function definition.

For example:

srand();

I want to find out exactly where the function srand() is defined.

The only method I can come up with is manually checking all the header includes to recursively find the declaration and the corresponding file for the definition.

This is a linux environment and the source is compiled with g++ (using cmake).

like image 849
Christopher Dorian Avatar asked Oct 21 '25 03:10

Christopher Dorian


2 Answers

Look into the ctags program.

ctags generates a file with information on the locations in the code where each identifier is used. It is then possible to use a plugin for your editor of choice that will allow you to jump to the definition of any identifier. (very mature ctags plugins exist for Vim and Emacs, and probably for most other popular editors as well.)

like image 145
Brennan Vincent Avatar answered Oct 23 '25 19:10

Brennan Vincent


If you are using VI you can shift+k that will open it in a man page, if there is one for this word. I believe it automatically is set to search in man(3) but i'm not 100% sure. At least this way you can know wether it's a system function or not.

Another option is to use Doxygen to setup the call graphs and let it generate the source (even if there are no comments). With the call graph you can easily spot from where each function is coming from.

like image 21
RedX Avatar answered Oct 23 '25 20:10

RedX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!