Any program or trick to find a variable definition?

Many times, when I look at the code of others, I just want to find where and how a variable is defined. Usually, I’m now looking for a variable type until I find a definition that takes a lot of time. And I think there are some tools that can help me in this routine situation. Any suggestion on some tools or commands that will help me in this task?

I know that using the graphical interface and creating a project, this is done automatically. I am talking about how to do this without a GUI. I only work with text mode. I work under Linux and I use C / C ++, but suggestions for other languages ​​are welcome.

Many thanks.

Possible Solution

Michel in one of his comments offers a simple effective solution to define the variable again, in this case, during compilation, the compiler will tell you where the previous criterion is. Of course, in order to apply this solution, we need to think earlier in the locality of the variable.

+7
c ++ variables definition find
source share
8 answers

Edit: Well, you say you use C ++. I am editing my answer. I would use the C preprocessor and then grep for the variable. He will appear first.

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

The C preprocessor combines all the programs included in it, and the definition must be before any use of this variable in the file. Not an ideal thing, but without an IDE or a complete language description / management tool, you only have text.

Another option would be to use ctags . He understands the syntax of C and C ++ (among others), and they can be searched for for variables and functions using command line tools, emacs and vi, among others.

+8
source share

You have already provided the most suitable tool: IDE. This is exactly what the IDE stands out for. Why don't you want to use the IDE if you find development painful without one?

Note that Emacs, Vim, etc. can work as an IDE - I'm not talking about making you into the GUI world if you want to stay in a textual situation, for example. because you are using sshing.

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

+11
source share

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

This is similar to the facilities provided by the mega-IDE, such as Visual Studio and Eclipse.

Cscope also functions as a standalone tool that performs these searches.

+5
source share

I use one of three methods:

  • I will use CTags to process my source tree (nightly), and then it is easy to use commands in Vim (or other editors) to get to the definition.
  • I just use grep (linux) or findstr (windows) to search for all occurrences of the variable name or type. The definition is usually pretty obvious.
  • In Vim, you can simply search back in the area and often find what you are looking for.
+2
source share

Grep for generic templates for variable declarations. Example: *, &,> or alphanumeric followed by one or more whitespace characters, followed by the name of the variable. Or the name of a variable followed by zero or more whitespace, then the left bracket or semicolon. If it was not defined in really strange circumstances (for example, with some kind of macro), it works every time.

+1
source share

In VIM, you can use gd to see local variable declarations or gd to see global variable declarations, if defined in the current file. Go_to_definition_using_g Link

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

+1
source share

If you work in Microsoft Visual Studio (which, I think, you could use for C ++, but to work on a Windows workstation), there is an accessible right-click menu for "Go to definition ...", which will lead you to the definition of any current labeled variable, type or method.

-one
source share

if you insist on saving text mode, you can do this with emacs or vi with the appropriate plugins.

But really, go to 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 this case, mount the disk on the local computer and use the IDE and just SSH to start the build.

-one
source share

All Articles