How can I quickly search all included header files in a project for a specific symbol?

我们两清 提交于 2019-12-01 01:08:18

问题


Here's a problem I've had recently that just HAS to be a common pain to others here.

I'm working with someone else's legacy C code and need to find where a function or macro was defined. The code #includes a bunch of different standard system libraries in addition to those from the specific project.

Is there a tool or technique to quickly find where a specific function, macro, (or other global for that matter) was defined?

I tried:

grep -R 'function' /usr/lib

and other similar *nix/bash-fu with only limited success and lots of annoying chaff to cull. One of you sage coders out there must have a good solution to this seemingly common scenario.

I was very surprised to not find another question on this particular pain here or in my searches of the interwebs. (I'm sure there will be angry comments if I missed one... ;-))

Thanks in advance for any tips!


回答1:


Use etags/ctags from the exuberant ctags project in conjunction with an editor (emacs, vim) that understands them, or GNU GLOBAL.

Oh, and if you happen to use automake it generates a target TAGS. So no need for complicated manual calls to {c,e}tags.




回答2:


Use ctags/cscope + vim/emacs

you can google for their detail use.

if you use ctags + vim, you can :

1.go to the /usr/include directory, excute ctags -f tags1 -R . generate the tags

2.generate tags for your code in your code directory ctags -f tags2 -R.

3.run :set path+=tags1,tags2 in your vim

4.under a function or marco try CTRL+]




回答3:


Here is what you can do, assuming you use gcc, if not just modify it accordingly.

gcc -E myfile.c | grep '^#' | cut -f 3 -d ' ' | sort |uniq | xargs -n 1 grep -l "MYMACROORFUNCTIONNAME"



回答4:


You can use Eclipse CDT. For example here is described how to setup CDT project to navigate Linux kernel source - HowTo use the CDT to navigate Linux kernel source.




回答5:


vim + ctags is the way to go. You can jump to and definition of functions, global variables, macros, etc. etc.

FYI, browsing programs with tags

Also, if you want to quickly switch between .c and .h files, please refer to this blog




回答6:


you can use cscope or emacs/vim + xcscope.el to do that easily. I think it's batter than ctage and etage.




回答7:


Provided the correct headers are included that directly or indirectly define what you look for, most IDEs have a jump-to-definition-functionality that works.

The tags-approaches are of course nice because they don't depend on correctly included headers.



来源:https://stackoverflow.com/questions/9888245/how-can-i-quickly-search-all-included-header-files-in-a-project-for-a-specific-s

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