简体   繁体   English

在源代码中查找引用的函数/变量定义

[英]Find function/variable definition for a reference in source code

My project contains many files.我的项目包含许多文件。

Sometimes I need to know where a particular function is defined (implemented) in source code.有时我需要知道特定函数在源代码中的定义(实现)位置。 What I currently do is text search within source files for the function name, which is very time consuming.我目前所做的是在源文件中搜索函数名称的文本,这非常耗时。

My question is: Is there a better way (compiler/linker flag) to find that function definition in source files?.... Since the linker has gone through all the trouble of resolving all these references already.我的问题是:有没有更好的方法(编译器/链接器标志)在源文件中找到该函数定义?....因为链接器已经经历了解析所有这些引用的所有麻烦。

I am hoping for method better than stepping into a function call in debugger, since a function can be buried within many calls.我希望有比在调试器中进入函数调用更好的方法,因为一个函数可以隐藏在许多调用中。

Try cscope utility .尝试cscope 实用程序

From the manual:从手册:

Allows searching code for:允许搜索代码:

  • all references to a symbol所有对符号的引用
  • global definitions全局定义
  • functions called by a function函数调用的函数
  • functions calling a function函数调用函数
  • text string文本串
  • regular expression pattern正则表达式模式
  • a file一份文件
  • files including a file文件包括文件

  • Curses based (text screen)基于诅咒(文本屏幕)

  • An information database is generated for faster searches and later reference生成信息数据库,以便更快地搜索和以后参考
  • The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)模糊解析器支持 C,但足够灵活,可用于 C++ 和 Java,并可用作通用的“grep 数据库”(用它来浏览大型文本文档!)
  • Has a command line mode for inclusion in scripts or as a backend to a GUI/frontend具有用于包含在脚本中或作为 GUI/前端的后端的命令行模式
  • Runs on all flavors of Unix, plus most monopoly-controlled operating systems.可以在所有类型的 Unix 以及大多数垄断控制的操作系统上运行。

A "screenshot":一个“截图”:

C symbol: atoi

  File     Function     Line
  0 stdlib.h <global>      86 extern int atoi (const char *nptr);
  1 dir.c    makefilelist 336 dispcomponents = atoi(s);
  2 invlib.c invdump      793 j = atoi(term + 1);
  3 invlib.c invdump      804 j = atoi(term + 1);
  4 main.c   main         287 dispcomponents = atoi(s);
  5 main.c   main         500 dispcomponents = atoi(s);
  6 stdlib.h atoi         309 int atoi (const char *nptr) __THROW



  Find this C symbol:
  Find this global definition:
  Find functions called by this function:
  Find functions calling this function:
  Find this text string:
  Change this text string:
  Find this egrep pattern:
  Find this file:
  Find files #including this file:

If the symbol is exported, then you could wire up objdump or nm and look at the .o files.如果符号已导出,那么您可以连接objdumpnm并查看.o文件。 This is not useful for finding things in header files though.但是,这对于在头文件中查找内容没有用。

My suggestion would be to put your project in git (which carries numerous other advantages) and use git grep which looks only at those files under git's revision control (meaning you don't grep object files and other irrelevances).我的建议是将您的项目放在git (它具有许多其他优点)并使用git grep ,它仅查看 git 修订控制下的那些文件(意味着您不会grep目标文件和其他无关文件)。 git grep is also nice and quick. git grep也很好而且很快。

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

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