简体   繁体   中英

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.

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.

You can use c-tags .

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

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 Cc 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 Cc sa , telling Cscope where to find cscope.out . Then the key bindings mentioned above should work.

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

ie

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.

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.

更新的方法是遵循本指南中的方法,其中包含几种跳转源树的方法,并适用于具有数十万个文件的大型项目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM