简体   繁体   English

在大型项目中按名称查找函数的最佳方法是什么?

[英]What is the best way to find a function by name in a huge project?

Let's say we have a project (eg a library) consisting of 100 files, each contains about 1000 lines of code. 假设我们有一个包含100个文件的项目(例如,一个库),每个文件包含大约1000行代码。 You want to find the function awesome_foo(...) How do you do it? 您想找到函数awesome_foo(...)怎么做? Honestly, I find grepping it creepy and ineffective... 老实说,我发现它令人毛骨悚然且无效。

EDIT: I'm looking mainly for the function definition 编辑:我主要是在寻找功能定义

grep -irnw "awesome_foo" * definitly will give you result. grep -irnw "awesome_foo" *绝对会给您结果。

The parameters are: 参数为:

-i, --ignore-case -i,--ignore-case

Ignore case distinctions in both the PATTERN and the input files. 忽略PATTERN和输入文件中的大小写区别。

-n, --line-number -n,--line-number

Prefix each line of output with the line number within its input file. 在输出的每一行之前,在其输入文件中添加行号。

-R, -r, --recursive -R,-r,-递归

Read all files under each directory, recursively; 递归读取每个目录下的所有文件; this is equivalent to the -d recurse option. 这等效于-d recurse选项。

-w, --word-regexp -w,--word-regexp

Select only those lines containing matches that form whole words. 仅选择包含构成整个单词的匹配项的行。 The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word con- stituent character. 测试是匹配的子字符串必须在行的开头,或者在非单词组成字符的前面。 Similarly, it must be either at the end of the line or followed by a non-word constituent character. 同样,它必须在行的末尾,或后跟非单词组成字符。 Word- constituent characters are letters, digits, and the underscore. 单词构成的字符是字母,数字和下划线。

if you can use something like notepadd++ that has a great find in files tool that will return all instances of a direct text search or even a regex. 如果您可以使用诸如notepadd ++之类的在文件中具有出色查找功能的工具,该工具将返回直接文本搜索甚至正则表达式的所有实例。 i use this a lot and its very helpful. 我经常使用它,它非常有帮助。

Integrated development environments parse your code and generate an index that allows you to navigate to each symbol's declaration (or usages) by ctrl+clicking on it, or using some keyboard shortcut. 集成开发环境解析您的代码并生成一个索引,该索引使您可以通过ctrl +单击它或使用某些键盘快捷键来导航到每个符号的声明(或用法)。 That's how people often work on large codebases. 这就是人们经常在大型代码库上工作的方式。

If you aren't using an IDE, you still have some options. 如果您不使用IDE,则仍有一些选择。

CTags is an indexer that read source files and generates "index files" which contain symbol information for your program. CTags是一个索引器,可以读取源文件并生成“索引文件”,其中包含程序的符号信息。 It can be hooked up into an editor (such as Vim) or just generate a human-readable symbol reference. 可以将其连接到编辑器(例如Vim)中,或仅生成人类可读的符号引用。

This is the output of ctags -x for an example program : 这是示例程序ctags -x的输出:

DrawGLScene        37 lesson5.c        void DrawGLScene()
InitGL             15 lesson5.c        void InitGL(int Width, int Height)    // We call this right after our OpenGL window is created.
ReSizeGLScene      24 lesson5.c        void ReSizeGLScene(int Width, int Height)
keyPressed         62 lesson5.c        void keyPressed(unsigned char key, int x, int y)
main               78 lesson5.c        int main(int argc, char **argv)

It looks like "name, line and file, signature" and can be easily used as an index manually. 它看起来像“名称,行和文件,签名”,并且可以轻松地手动用作索引。

−x -x

Print a tabular, human-readable cross reference (xref) file to standard output instead of generating a tag file. 将表格格式的人类可读交叉引用(xref)文件打印到标准输出,而不是生成标记文件。 The information contained in the output includes: the tag name; 输出中包含的信息包括:标签名称; the kind of tag; 标签的种类; the line number, file name, and source line (with extra white space condensed) of the file which defines the tag. 定义标签的文件的行号,文件名和源行(带有多余的空格)。 No tag file is written and all options affecting tag file output will be ignored. 没有标记文件被写入,所有影响标记文件输出的选项都将被忽略。 Example applications for this feature are generating a listing of all functions located in a source file (eg ctags −x −−c−kinds=f file), or generating a list of all externally visible global variables located in a source file (eg ctags −x −−c−kinds=v −−file−scope=no file). 此功能的示例应用程序是生成位于源文件中的所有功能的列表(例如ctags -x -c-kinds = f文件),或生成位于源文件中的所有外部可见的全局变量的列表(例如ctags) -x -c-kinds = v -file-scope =无文件)。 This option must appear before the first file name. 此选项必须出现在第一个文件名之前。

Use eclipse IDE. 使用eclipse IDE。 You can track functions in navigator. 您可以在导航器中跟踪功能。 CodeInsight is also fine. CodeInsight也很好。 Just add a project to add keep track of your code. 只需添加一个项目即可添加对代码的跟踪。 Believe me it will save your time. 相信我,它将节省您的时间。

The ctags /etags programs are useful under linux and cygwin environments. ctags / etags程序在linux和cygwin环境下很有用。 They have bindings for editors like vim and emacs as well as other tools. 它们具有vim和emacs等编辑器的绑定以及其他工具。 It lets you select functions and can then move back and forth between declarations and implementations, among other things. 它使您可以选择函数,然后可以在声明和实现之间来回移动。

another possibility is astro grep which is a windows aplication which gives you a gui to form your grep statements, taking the 'sting' out and making the process of forming complicated searches very simple. 另一种可能性是astro grep ,它是Windows应用程序,它为您提供了一个gui来形成grep语句,消除了“刺痛”,并使形成复杂搜索的过程非常简单。

"features: - Regular expressions (Uses the standard Microsoft .Net Regular Expressions: Quick Reference) - Concurrent multiple file types - Recursive directory searching - A "context" feature that selects the lines above and below your search expression - Most Recently Used list for search paths - Somewhat versatile printing options - Double click to open file with editor of your choice - Store Most Recently Used file names and search expressions - Match Whole Word Only - Syntax highlighing - Free of charge and Open Source" “功能:-正则表达式(使用标准的Microsoft .Net正则表达式:快速参考)-并发多种文件类型-递归目录搜索-一种“上下文”功能,用于选择搜索表达式上方和下方的行-最近使用的列表搜索路径-某种程度的多功能打印选项-双击以您选择的编辑器打开文件-存储最近使用的文件名和搜索表达式-仅匹配整个单词-语法高亮-免费且开源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM