Amumu
Amumu

Reputation: 18552

How to find the source code from header declaration?

I want to browse the source code in of the symbols in my header files in /usr/include/ . For example, in netdb.h, there's a function named getaddrinfo(...). I want to know where the source code is stored at least by using the shell.

I will be more appreciate if you tell me how to do it specifically in Emacs and/or Cscope.

Upvotes: 3

Views: 1653

Answers (7)

Junmin Yu
Junmin Yu

Reputation: 1

If you are using emacs and etags, you can make TAGS file including header declaration.

$> etags -e -L {file_list} --c-kind=+px

p means prototype and x means external variable.

Then you can see a function's definition and prototype definition in header to hit M-.

Recent emacs has xref feature, so xref-find-definition will show 2 line for definition and prototype in header.

Upvotes: 0

Amumu
Amumu

Reputation: 18552

More up to date way is to follow the methods in this guide that contains several methods to jump around source tree, and works with large projects that have several dozens of thousands of files.

Upvotes: 0

James Kanze
James Kanze

Reputation: 153909

It's curious that no one else mentions it, but... in order to view the source code, you have to have it. On most Unices (all but Linux), such sources are proprietary; to have access to them, you need a special license, or to work for the firm. In the case of Linux, the sources are publicly available, but typically won't be installed on the machine. If it's your machine, and you have the DVD used to install the system, the sources should be on it, usually in a separate package. If you don't have the installation DVD, or you can't install packages on the system (the case of most people using Linux professionally), you'll have to download them from the internet somewhere.

Upvotes: 1

Daimrod
Daimrod

Reputation: 5020

As said before you could use etags to generate tags for the specified sources.

i.e.

find /usr/include/ -type f -name \*.h -exec etags --append -o INCLUDE_TAGS {\} \;

This will create a file named INCLUDE_TAGS which contains the location of every definition in the headers located in /usr/include/.

Then you can use it with M-. and when it asks for the TAGS file use INCLUDE_TAGS. It takes some times the first time but after it's fast.

However you'll just jump to the definition in the header not in the source, if you want to do so you'll have to download the sources and generate its TAGS.

Upvotes: 1

The definition (with the function body) of standard C library's functions is (usually on Linux) inside the GNU Libc. Several functions (those in the section 2 of man pages for syscalls, with syscall numbers listed in <asm/unistd.h>) are tiny wrappers to system calls, so the actual work is done inside the linux kernel whose source is on kernel.org. For instance, write(2) is a system call (mostly done in the kernel) which may be used by the printf(3) library function (whose code is in GNU Libc). You could install GNU Libc source code and use ctags to find symbols there.

The declaration of standard C library's functions are in header files under /usr/include, so utilities like ctags can help you find them.

Upvotes: 3

Lei Mou
Lei Mou

Reputation: 2582

Have a look at the following text (which comes from xcscope.el) if you plan to use cscope in Emacs.

* Keybindings:

All keybindings use the "C-c s" prefix, but are usable only while
editing a source file, or in the cscope results buffer:

    C-c s s         Find symbol.
    C-c s d         Find global definition.
    C-c s g         Find global definition (alternate binding).
    C-c s G         Find global definition without prompting.
    C-c s c         Find functions calling a function.
    C-c s C         Find called functions (list functions called from a function).
    C-c s t         Find text string.
    C-c s e         Find egrep pattern.
    C-c s f         Find a file.
    C-c s i         Find files #including a file.

You can find more information in that file. The following link might also be helpful:

Before finding a symbol using Cscope, you should first create an index by C-c s I at the root directory of your code base, for example, a folder called foo. Then two files will be generated. Cscope will find all the source file contained in foo recursively, and create a list called cscope.files. Then it will use this list to create an index for all the symbols in each file, and store this information in file cscope.out. After that, just set the initial directory of cscope to foo by pressing C-c s a, telling Cscope where to find cscope.out. Then the key bindings mentioned above should work.

Upvotes: 2

Alok Save
Alok Save

Reputation: 206526

You can use c-tags.

C-] - go to definition
C-T - Jump back from the definition.
C-W C-] - Open the definition in a horizontal split

Upvotes: 2

Related Questions