3

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Omar Darwish
  • 1,536
  • 2
  • 15
  • 23
  • 2
    Probably just write a dirty grep haha – AndyG Sep 19 '19 at 22:56
  • 2
    There are some nice tools out there like *ctags* and *cscope* that are helpful in exploring code. – Christian Gibbons Sep 19 '19 at 22:57
  • Closest thing to a standard tool I can think of puts me in the `grep` camp with @AndyG – user4581301 Sep 19 '19 at 23:02
  • Is this a bad question? If so, what is the proper place to ask questions like this? – Omar Darwish Sep 19 '19 at 23:05
  • 2
    Are you using an IDE? They usually have a "Go to definition" function. Visual Studio Code, Code::Blocks, Eclipse, Qt Creator and CLion all offer it. And if what you are looking for is part of the standard library, even sites like cppreference.com can help you. – Fabio says Reinstate Monica Sep 19 '19 at 23:13
  • No, I'm using Vim. I'm mainly interested in figuring out what part of the standard library declared a "thing" in the source I'm looking at – Omar Darwish Sep 19 '19 at 23:15
  • How about using [`ctags`](http://ctags.sourceforge.net/)? I use it with vim and it works like a charm. See for instance [here](https://linuxhint.com/vim_ctags/) – Sigve Karolius Sep 19 '19 at 23:16
  • This isn't that bad a question, in my view, but it is a Tool or Library Request question, and they are forbidden here. – user4581301 Sep 19 '19 at 23:33
  • 1
    Why don't you think `man` documentation is authoritative! That would seem like the most logicl place to put that documentation so that it is in the same-place on all systems. – Martin York Sep 20 '19 at 00:27
  • @MartinYork I do, but as far as I'm aware you can't do `man `, right? – Omar Darwish Sep 20 '19 at 01:01
  • 2
    Possible duplicate of [How can I quickly search all included header files in a project for a specific symbol?](https://stackoverflow.com/q/9888245/608639), [How do I find declarations/definitions without an IDE?](https://stackoverflow.com/q/43004338/608639), [How to determine which include header file a function comes from?](https://stackoverflow.com/q/21064039/608639), etc. – jww Sep 20 '19 at 01:33
  • Friends, is there anything required from the when this question is on hold? – Omar Darwish Sep 20 '19 at 15:37

1 Answers1

4

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.

Nathan Dorfman
  • 331
  • 1
  • 3
  • Note: on Ubuntu, the default `ctags` does not define `-R` as `--recurse`. Install exuberant-ctags which supports this option with `sudo apt-get install ctags` – Omar Darwish Sep 19 '19 at 23:58
  • This also leads to you `#includeing` platform specific header files. You should not use this technique to decide what header files are required (only to peak at the implementation (though why you want to do that is just as strange)). Use the man page to decide what header files to include. – Martin York Sep 20 '19 at 00:26
  • Note: must package installation managers will install "extra" libraries in `/usr/local/include` – Martin York Sep 20 '19 at 00:30
  • 1
    @MartinYork my understanding of the question is that it's about lookup up function signatures, typedefs and constants while reading existing code. – Nathan Dorfman Sep 20 '19 at 02:12