有什么程序或技巧可以找到变量的定义吗?

发布于 2024-07-14 07:24:06 字数 376 浏览 6 评论 0原文

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

我知道使用 GUI 并创建项目是自动完成的,我正在讨论一种无需 GUI 即可完成此操作的方法。 我仅使用文本模式。 我在 Linux 下运行,使用 C/C++,但欢迎提供其他语言的建议。

多谢。

一个可能的解决方案

米歇尔在他的评论中提出了一个简单有效的解决方案,重新定义变量,在这种情况下,在编译时,编译器会告知以前的定义在哪里。 当然,要应用这个解决方案,我们需要事先考虑变量的局部性。

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?.

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.

Thanks a lot.

A possible solution

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

魂牵梦绕锁你心扉 2024-07-21 07:24:06

您已经提供了最合适的工具:IDE。 这正是 IDE 所擅长的事情。 如果您发现没有 IDE 开发会很痛苦,为什么您不想想要使用 IDE?

请注意,Emacs、Vim 等可以作为 IDE 工作 - 如果您想保持纯文本状态,例如因为您正在通过 SSH 连接,我并不是在谈论强迫您进入 GUI 世界。

(我真的不是在这里试图粗鲁,我只是认为你已经忽略了明显的解决方案而没有解释原因。)

You've already given the most appropriate tool: an IDE. This is exactly the kind of thing which an IDE excels at. Why would you not want to use an IDE if you're finding development painful without one?

Note that Emacs, Vim etc can work as IDEs - I'm not talking about forcing you the world of GUIs if you want to stay in a text-only situation, e.g. because you're SSHing in.

(I'm really not trying to be rude here. I just think you've discounted the obvious solution without explaining why.)

刘备忘录 2024-07-21 07:24:06

编辑:好的,你说你正在使用 C++。 我正在编辑我的回复。 我将使用 C 预处理器,然后使用 grep 作为变量。 它将首先出现。

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

C 预处理器将加入程序使用的所有包含内容,并且定义必须位于文件中对该变量的任何使用之前。 这不是一件完美的事情,但如果没有 IDE 或完整的语言描述/管理工具,您只有文本。

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

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

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.

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.

暖心男生 2024-07-21 07:24:06

我虔诚地使用 cscope 和 ctags-exuberant。 在我的代码库上运行一次,然后在 Vim 中,我可以使用各种命令(例如 ^] 或 [D 或 [I 或类似命令)来查找给定单词的任何定义或声明。

这类似于 Visual Studio 和 Eclipse 等大型 IDE 提供的功能。

Cscope 还可以作为执行这些搜索的独立工具。

I use cscope and ctags-exuberant religiously. Run it once on my code base and then in Vim, I can use various commands like ^] or [D or [I or similar to find any definitions or declarations for a given word.

This is similar to facilities provided by mega-IDEs like Visual Studio and Eclipse.

Cscope also functions as a stand-alone tool that performs these searches.

浅笑依然 2024-07-21 07:24:06

我使用三种方法之一:

  1. 我将使用 CTags 处理我的源代码树(每晚)并然后可以轻松地使用 Vim(或其他编辑器)中的命令直接跳转到定义。
  2. 我将仅使用 grep (linux) 或 findstr (windows) 来查找所有出现的变量名称或类型。 定义通常非常明显。
  3. 在 Vim 中,您只需在范围内向后搜索,通常就能找到您要查找的内容。

I use one of three methods:

  1. I will use CTags to process my source tree (nightly) and then can easily use commands in Vim (or other editors) to jump right to the definition.
  2. I will just use grep (linux) or findstr (windows) to look for all occurrences of the variable name or type. The definition is usually quite obvious.
  3. In Vim, you can just search backward in the scope and often find what you are looking for.
如梦 2024-07-21 07:24:06

Grep 获取变量声明的常见模式。 示例:*、&、> 或者一个字母数字后跟一个或多个空白字符,然后是变量的名称。 或者变量名后跟零个或多个空白字符,然后是左括号或分号。 除非它是在非常奇怪的情况下定义的(例如某种宏),否则它每次都有效。

Grep for common patterns for variable declarations. Example: *, &, > or an alphanumeric followed by one or more whitespace characters then the name of the variable. Or variable name followed by zero or more whitespace characters, then a left parenthesis or a semicolon. Unless it was defined under really weird circumstances (like with some kind of macro), it works every time.

篱下浅笙歌 2024-07-21 07:24:06

在 VIM 中,您可以使用 gd 查看局部变量声明,或使用 gD 查看全局变量声明(如果它们在当前文件中定义)。 参考 Go_to_definition_using_g

也可以使用 [i 查看定义,无需跳转到它,或 [I 来查看所有包含的文件中该变量的所有出现,这自然也会显示定义。

In VIM you can use gd to see local variable declarations or gD to see global variable declarations, if they're defined in the current file. Reference Go_to_definition_using_g

You can also use [i to see the definition without jumping to it, or [I to see all occurrences of the variable in all the included files as well, which will naturally show the definition as well.

用心笑 2024-07-21 07:24:06

如果您使用 Microsoft Visual Studio(我认为您也可以将其用于 C++,但需要在 Windows 工作站上工作),则可以轻松访问“转到定义...”的右键单击菜单选项,这将需要您可以定义任何当前标记的变量、类型或方法。

If you work in Microsoft Visual Studio (which I think you could use for C++ as well, but would require working on a Windows workstation) there's an easily accessible right-click menu option for "Go to Definition...", which will take you to the definition of any currently marked variable, type or method.

鸵鸟症 2024-07-21 07:24:06

如果您坚持保持文本模式,可以使用 emacs 或 vi 以及适当的插件来完成此操作。

但实际上,进入 21 世纪。

编辑:您评论说您正在通过 SSH 执行此操作,因为您需要远程服务器集群的构建速度。

在这种情况下,将驱动器安装在本地计算机上并使用 IDE,然后只需通过 SSH 即可启动构建。

if you insist on staying text mode, you can do this with either emacs or vi with the appropriate plug-ins.

But really, move into the 21st century.

EDIT: You commented that you are doing this over SSH because you need the build speed of the remote server cluster.

In that case, mount the drive on your local machine and use an IDE, and just SSH in to kick off a build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文