Eduardo
Eduardo

Reputation: 21048

Any program or trick to find the definition of a variable?

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.

Upvotes: 9

Views: 8041

Answers (8)

Nathan Fellman
Nathan Fellman

Reputation: 127468

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.

Upvotes: 1

FlySwat
FlySwat

Reputation: 175603

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.

Upvotes: -1

greyfade
greyfade

Reputation: 25657

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.

Upvotes: 5

Steve Rowe
Steve Rowe

Reputation: 19413

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.

Upvotes: 2

Michel
Michel

Reputation: 1492

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.

Upvotes: 1

Diego Sevilla
Diego Sevilla

Reputation: 29021

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.

Upvotes: 8

Tomas Aschan
Tomas Aschan

Reputation: 60594

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.

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1500765

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

Upvotes: 9

Related Questions