Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to jump to a C/C++ function declaration instead of its definition?

Tags:

c++

c

vim

editor

I know CTRL+] to jump to definition of a function in various languages in vim. What about jump to declaration in C/C++ header?

Since I have plenty of headers containing meaningful comments/explanations, I would frequently find function declarations among a large code base. Is there some sorts of shortcuts to do this?

like image 461
Keisau CHING Avatar asked May 05 '14 06:05

Keisau CHING


People also ask

What is the difference between function definition and function declaration?

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

Can we call a function without declaration in C?

You can't call a function without declaring it first.

What happens when a function is called before its declaration in C?

In C, if a function is called before its declaration, the compiler assumes the return type of the function as int.

What are different ways to define a function in C?

Try it Yourself » A function consist of two parts: Declaration: the function's name, return type, and parameters (if any) Definition: the body of the function (code to be executed)


3 Answers

ctags does not add function prototypes by default. You have to add the option

--c++-kinds=+p

to support function prototypes. My ctags looks like this:

ctags --c++-kinds=+p --fields=+iaS --extra=+q *.h *.c

When you are at a tag, you can use the following editor commands to browser through the tags (Meaning the same tag occuring multiple times as definition/declaration)-

tn (Next Tag)

tp (Previous Tag)

like image 93
Habi Avatar answered Oct 08 '22 23:10

Habi


Based on Bram's 7 habits, without any plugin, you can do

1) Set the path to your headers(This is crucial to work with point 2 below)

:set path+=/path/to/headers

2) Then do one of the following command

[I Display all list where keyword under cursor is in header files or

[<Tab> will jump there and you can come back to your file using Ctrl+6

like image 45
dlmeetei Avatar answered Oct 08 '22 23:10

dlmeetei


I am satisfied with cscope, which includes header files in database.

Just put cscope_maps.vim in ~/.vim/plugin/ and then CTRL-] will list all choices if a cscope database is built.

To build a cscope database, just type

cscope -bR
like image 1
Keisau CHING Avatar answered Oct 09 '22 01:10

Keisau CHING