TOP KEK
TOP KEK

Reputation: 2661

Find function definition in C++

I have a C++ project on remote machine which compiles with makefile. No ide available, only vim and commandline. I found a function in the sourcecode and want to find its definition. How can I do that without manually looking all -I directories and source files? Is it possible to stop makefile after preprocessing phase and lookup in the resulting sourcefiles?

Upvotes: 4

Views: 4369

Answers (4)

paper.plane
paper.plane

Reputation: 1197

You can use ctag and cscop to browse the code on vi. If you are not comfortable with that then use a

grep -rn funtion_name ./

in the home dir of the source code, and manually look for the definition in the result set.

Upvotes: 5

Davidbrcz
Davidbrcz

Reputation: 2397

You can use several plugins for vin to do code browsing.

A friend of mine created its own plugin : lh-tags and he developed an indexer for vim for C and C++ base code with clang : CLIC (with its own helper package, vim-clang)

Upvotes: 1

xryl669
xryl669

Reputation: 3614

The easiest solution would be to use grep (with find to get results very fast) like this:

find srcDir -name "*.c" -o -name "*.cpp" -print0 | xargs -0 grep "functionName"

The Makefile based solution is to change the linking so you ask for a verbose location of a function definition like this (if you use GCC) (you can also do that on the command line with "make LDFLAGS=...seebelow...")

# In your makefile, locate this:
LDFLAGS:= ...
# Replace by this
LDFLAGS:= -Wl,--trace-symbol=functionName

This results in:

$ gcc -o test main.o -Wl,--trace-symbol=main
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: reference to main
main.o: definition of main

This solution is useful if you have plenty of similar function name, and you actually want the one that's used in the final binary.

Upvotes: 1

James Kanze
James Kanze

Reputation: 154047

I'm currently developing under Visual Studios, but I still use vim as my editor, and the Unix took kit (from CygWin) for just about everything but compiling and debugging.

First, of course, if you can't easily deduce where the function is declared and defined, there's something wrong with you source structure and naming conventions. Regretfully, most of us don't have full control, and the case does really occur.

If the function is in your source tree, you can search using grep, possibly with find to find the files to search. If it's not (which is often the case), you can generally output the preprocessor results by invoking the compiler with the -E option. You may want to define a target in make for this. On the other hand, the only compiler options you need here are the -I and -D options; if they aren't too complex, you can just invoke the compiler by hand. (Or you might consider defining an environment variable with them, so you can easily invoke the compiler by hand.) -E will usually output to standard out, so you'll have to redirect the output to a file. After than, it's easy to find the file where the function was declared, since the preprocessor output contains #line directives with the file name. As to where it is defined... if it's third party software, you may not even have the definition; vendors often furnish just headers and a library.

Upvotes: 1

Related Questions