How to find the callers and callee of a function in C code in vi/vim?

≯℡__Kan透↙ 提交于 2019-11-30 04:13:57

For that, Vim integrates with the cscope tool; see :help cscope for more information.

Ciro Santilli 新疆改造中心996ICU六四事件

cscope minimal example

Ingo mentioned it, here is an example.

Go to the base directory of your project and run:

cscope -Rb

This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.

Open vim and run:

:cs add cscope.out
:cs find c my_func

c is a mnemonic for callers. The other cscope provided queries are also possible, mnemonics are listed under:

help cscope

This adds a list of the callers to the quickfix list, which you can open with:

:copen

Go to the line that interests you and hit enter to jump there.

To find callers of the function name currently under the cursor, add to your .vimrc:

function! Csc()
  cscope find c <cword>
  copen
endfunction
command! Csc call Csc()

and enter :Csc<enter> when the cursor is on top of the function.

TODO:

A word of advice: I love vim, but it is too complicated for me to setup this kind of thing. If a project matters enough to you, try to get the project working on some "IDE". It may involve some overhead if the project does not track the IDE configuration files (which are auto-changing blobs that pollute the repo...), but it is worth it to me. For C / C++, my favorite so far was KDevelop 4.

vi / . --- / is the search function in vi, and . will repeat the same command.

you could also use sed ( stream editor ) if it is a large file sed grep can get you the line numbers

read the man page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!