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

前端 未结 8 1067
梦谈多话
梦谈多话 2021-01-02 09:01

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 t

8条回答
  •  失恋的感觉
    2021-01-02 09:46

    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.

提交回复
热议问题