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 #include
s a bunch of different standard system libraries in addition to those from the specific project.
我正在处理别人的遗留C代码,需要找到函数或宏的定义。代码#包含了一些不同的标准系统库,除了那些特定的项目。
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.
还有其他类似的“nix/bash-fu”,只有有限的成功,还有很多烦人的小玩意儿要剔除。你们中有一个聪明的程序员必须有一个很好的解决方案来解决这个看起来很常见的问题。
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!
提前谢谢你的建议!
7 个解决方案
#1
4
Use etags
/ctags
from the exuberant ctags project in conjunction with an editor (emacs, vim) that understands them, or GNU GLOBAL.
与理解它们的编辑器(emacs, vim)或GNU GLOBAL一起使用旺盛的ctags项目中的etags/ctags。
Oh, and if you happen to use automake it generates a target TAGS
. So no need for complicated manual calls to {c,e}tags
.
哦,如果你碰巧使用automake,它会生成目标标签。因此不需要对{c,e}标签进行复杂的手动调用。
#2
1
Use ctags/cscope + vim/emacs
使用ctags / cscope + vim / emacs
you can google for their detail use.
您可以谷歌进行详细的使用。
if you use ctags + vim, you can :
如果您使用ctags + vim,您可以:
1.go to the /usr/include
directory, excute ctags -f tags1 -R .
generate the tags
1。转到/usr/include目录,exlovely ctags -f tags1 -R。生成标签
2.generate tags for your code in your code directory ctags -f tags2 -R
.
2。在代码目录ctags -f tags2 -R中为代码生成标记。
3.run :set path+=tags1,tags2
in your vim
3.run:set path+=tags1,在vim中设置tags2
4.under a function or marco try CTRL+]
4所示。在函数下或marco尝试CTRL+]
#3
1
Here is what you can do, assuming you use gcc, if not just modify it accordingly.
这里是您可以做的,假设您使用gcc,如果不是相应地修改的话。
gcc -E myfile.c | grep '^#' | cut -f 3 -d ' ' | sort |uniq | xargs -n 1 grep -l "MYMACROORFUNCTIONNAME"
#4
0
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.
您可以使用Eclipse CDT。例如,本文描述了如何设置CDT项目来导航Linux内核源代码——如何使用CDT导航Linux内核源代码。
#5
0
vim + ctags is the way to go. You can jump to and definition of functions, global variables, macros, etc. etc.
vim + ctags是可行的。您可以跳转到函数、全局变量、宏等的定义。
FYI, browsing programs with tags
供参考,有标签的浏览程序
Also, if you want to quickly switch between .c and .h files, please refer to this blog
另外,如果您想要在.c和.h文件之间快速切换,请参考本博客
#6
0
you can use cscope or emacs/vim + xcscope.el
to do that easily. I think it's batter than ctage and etage.
您可以使用cscope或emacs/vim + xcscope。这样做很容易。我认为它比ctage和etage更合适。
#7
0
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.
如果包含了直接或间接定义查找内容的正确头,大多数ide都具有可工作的从定义到定义的功能。
The tags-approaches are of course nice because they don't depend on correctly included headers.
标记方法当然很好,因为它们不依赖于正确包含的标题。
#1
4
Use etags
/ctags
from the exuberant ctags project in conjunction with an editor (emacs, vim) that understands them, or GNU GLOBAL.
与理解它们的编辑器(emacs, vim)或GNU GLOBAL一起使用旺盛的ctags项目中的etags/ctags。
Oh, and if you happen to use automake it generates a target TAGS
. So no need for complicated manual calls to {c,e}tags
.
哦,如果你碰巧使用automake,它会生成目标标签。因此不需要对{c,e}标签进行复杂的手动调用。
#2
1
Use ctags/cscope + vim/emacs
使用ctags / cscope + vim / emacs
you can google for their detail use.
您可以谷歌进行详细的使用。
if you use ctags + vim, you can :
如果您使用ctags + vim,您可以:
1.go to the /usr/include
directory, excute ctags -f tags1 -R .
generate the tags
1。转到/usr/include目录,exlovely ctags -f tags1 -R。生成标签
2.generate tags for your code in your code directory ctags -f tags2 -R
.
2。在代码目录ctags -f tags2 -R中为代码生成标记。
3.run :set path+=tags1,tags2
in your vim
3.run:set path+=tags1,在vim中设置tags2
4.under a function or marco try CTRL+]
4所示。在函数下或marco尝试CTRL+]
#3
1
Here is what you can do, assuming you use gcc, if not just modify it accordingly.
这里是您可以做的,假设您使用gcc,如果不是相应地修改的话。
gcc -E myfile.c | grep '^#' | cut -f 3 -d ' ' | sort |uniq | xargs -n 1 grep -l "MYMACROORFUNCTIONNAME"
#4
0
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.
您可以使用Eclipse CDT。例如,本文描述了如何设置CDT项目来导航Linux内核源代码——如何使用CDT导航Linux内核源代码。
#5
0
vim + ctags is the way to go. You can jump to and definition of functions, global variables, macros, etc. etc.
vim + ctags是可行的。您可以跳转到函数、全局变量、宏等的定义。
FYI, browsing programs with tags
供参考,有标签的浏览程序
Also, if you want to quickly switch between .c and .h files, please refer to this blog
另外,如果您想要在.c和.h文件之间快速切换,请参考本博客
#6
0
you can use cscope or emacs/vim + xcscope.el
to do that easily. I think it's batter than ctage and etage.
您可以使用cscope或emacs/vim + xcscope。这样做很容易。我认为它比ctage和etage更合适。
#7
0
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.
如果包含了直接或间接定义查找内容的正确头,大多数ide都具有可工作的从定义到定义的功能。
The tags-approaches are of course nice because they don't depend on correctly included headers.
标记方法当然很好,因为它们不依赖于正确包含的标题。