views:

655

answers:

8

Hello,

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.

+6  A: 

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.

Diego Sevilla
In addition to ctags, Semantic is another add-on that Emacs users might want to check out: http://cedet.sourceforge.net/semantic.shtml
codelogic
Thanks, codelogic. Yes, I just discovered it and giving it a try :) Thanks again.
Diego Sevilla
+9  A: 

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

Jon Skeet
The reason why I discounted that one, it is because normally I use GUIs but now I am working in a little project in only SSHing mode and I am just looking for something simple. You did not look rude. Thx
Eduardo
In that case I'd use Emacs as an IDE. I would be surprised if it didn't have a "go to definition" somewhere.
Jon Skeet
AFAIK, Emacs doesn't have such functionality out of the box, although ctags and Semantic, two add-ons, do provide it.
codelogic
100% agree. Relevant links: http://www.linuxjournal.com/article/5765 http://frodo.syminet.com/~deep/emacside.html
Chuck
A: 

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.

Tomas Lycken
In the question I specify no GUIs because I am working in text mode
Eduardo
Not to mention that you left out a word: maybe. I haven't found that to be the least bit reliable. Sometimes it works and sometimes it doesn't.
David Thornley
A: 

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.

FlySwat
Mounting the drive on the local machine is sometimes not possible. I often work from home and have to SSH in because our sysadmin won't let people mount the drives from outside. So I use emacs, too.
Can you set up a remote X connection tunneled through ssh?
Mr Fooz
@Mr Fooz if you are asking to me, no idea, also I do not wanna get in troubles with the admin I do not have a very good relation with him
Eduardo
Bear in mind that compiling can involve a lot of data transfer, which is often best done with a local machine rather than encrypted over a communications channel of unspecified speed.
David Thornley
+1  A: 

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.

Michel
Yeah normally I do that, but I was looking for something more "professional"
Eduardo
You could always redefine the variable and wait for a compile error to tell you where the previous declaration was. Anything more professional would probably just be "use an IDE."
Michel
That's a good trick
Eduardo
+4  A: 

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.

greyfade
+2  A: 

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.
Steve Rowe
+1  A: 

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.

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.

Nathan Fellman