找到变量定义的任何程序或技巧?

2022-01-20 00:00:00 variables find c++ definition

很多时候,当我在看其他人的代码时,我只想找到变量的定义位置和方式.通常我现在所做的是查找变量的类型,直到找到定义,这非常耗时.而且我想有一些工具可以帮助我解决这种日常情况.对一些工具或命令有什么建议可以帮助我完成这项任务吗?

Many times when I am watching others code I just want to find where and how a variable is defined. Normally what I do now is look for the type of the variable until I find the definition, that is very time consuming. And I guess that there are some tools that can help me in this rutinary situation. Any suggestion in some tools or commands to help me in this task?.

我知道使用 GUI 并创建一个项目是自动完成的,我说的是一种无需 GUI 的方法.我只使用文本模式.我在 Linux 下运行,我使用 C/C++,但欢迎提出其他语言的建议.

I know that using a GUI and creating a project this is done automatically I am talking of a way to do this without a GUI. I am working with only text mode. I am running under Linux and I am using C/C++, but suggestions for other languages are welcome.

非常感谢.

可能的解决方案

Michel 在他的一个评论中提出了一个简单有效的解决方案,再次定义变量,在这种情况下,在编译时,编译器将通知之前的定义在哪里.当然,要应用这个解决方案,我们需要事先考虑变量的局部性.

Michel in one of his comments propose a simple an effective solution define again the variable, in that case in compilation time, the compiler will inform where is the previous definiton. Of course to apply this solution we need to think previously in the locality of the variable.

推荐答案

编辑:好的,你说你使用的是 C++.我正在编辑我的回复.我会使用 C 预处理器,然后使用 grep 获取变量.它会首先出现.

Edit: OK, you say you're using C++. I'm editing my response. I would use the C preprocessor and then grep for the variable. It will appear in the first place.

cpp -I...(preprocessor options here) file.cpp | grep variable

C 预处理器将加入程序使用的所有包含,并且定义必须在文件中对该变量的任何使用之前.不是一个完美的东西,但没有 IDE 或完整的语言描述/管理工具,你只有文本.

The C preprocessor will join all the includes that the program uses, and the definition has to be before any usage of that variable in the file. Not a perfect thing, but without an IDE or a complete language description/managing tool, you only have the text.

另一种选择是使用 ctags.它了解 C 和 C++ 语法(以及其他),并且可以使用命令行工具、emacs 和 vi 等搜索变量和函数.

Another option would be using ctags. It understands the C and C++ syntaxes (among others), and can be searched for variables and functions using command line tools, emacs and vi, among others.

相关文章