Skip to content
Advertisement

How do I find out which header/include declared what variables in source? [closed]

If I see a struct, typedef, const, or any other variable being used which was not explicitly declared in the source that I’m reading, is there a standard way in Linux to go about discovering which of the source’s included header files provided the variable declaration?

I’ve been googling each individual header and hoping that the thing I’m looking for is declared, but there has to be a better way!

I know you can man <standard_func>, but this seems too broad for finding out where something was declared. Is there authoritative documentation provided within the system or on a site?

Advertisement

Answer

Since you are already using vim, I recommend installing the ctags package, which will allow you to jump to those declarations directly within the editor.

Once installed, create a ctags file. This will parse all the source code files in the specified paths, and create a file named tags:

ctags -R /usr/include .

Note that we specify /usr/include as well as the current directory, which presumably contains the source code you’re reading. If that’s in a different directory, specify that one instead.

After that completes (give it a moment), open the file you’re interested in. If you’re still in the same directory as the new tags file, vim will have already loaded it. Otherwise you would have to :set tags=/path/to/tags.

Now just place the cursor over a symbol you don’t know, and press Ctrl-]. Alternatively, if you’re using the GUI version of vim, you can left-click while holding Ctrl. Either way, this will jump to the declaration of the symbol.

Press Ctrl-T to go back to where you were.

Once you get the hang of this, check out :help tags and :help tagstack. vim has extensive support for working with them.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement