how to find the header file to be included for a library function in linux

15,544

Solution 1

Man pages. Type man atoi (or, in general, man <function>) at your command prompt. It will give you usage information as well as a listing of which headers to include.

Man pages also document programs and commands (find, grep, cd, etc.). Sometimes you may run into a case where a program has the same name as a C function (e.g. write). In that case, you need to direct man to look in the correct section of the manual, section 2 for system calls and section 3 for library functions. You do this by inserting the section number between "man" and the command name: man 2 write. If you do not know whether a given function is a system call or a library function, try both.

You can learn more about manual pages by typing man man.

Solution 2

Or, you can search your system's /usr/include directory for occurrences of the function definition you're looking for. This is especially useful for embedded or stripped-down linux systems that are missing man pages.

find /usr/include -name "*.h" -print | xargs grep "<function-you-are-looking-for>"

For example, if you do:

find /usr/include -name "*.h" -print | xargs grep atoi

You'll get back something like this:

/usr/include/stdlib.h:extern int atoi (__const char *__nptr)

The result contains both the header file name and the interface definition.

  • Please note that your /usr/include directory might be elsewhere.

Solution 3

If you are using ctags and the vim editor and you have set up ctags to scan /usr/include then ctrl-] while you're on the function you want to find takes you to the headerfile!

Solution 4

Is a document for linux c api ?

Certainly. The documentation is available as man pages. Type man <function> in a terminal and enjoy. Which header file you need to include is usually shown at the top.

Share:
15,544

Related videos on Youtube

Haiyuan Zhang
Author by

Haiyuan Zhang

Updated on June 04, 2022

Comments

  • Haiyuan Zhang
    Haiyuan Zhang almost 2 years

    Given a function, let's say atoi, how can I find the header file I should include if I want to use this function ? I'm always get puzzled for that issue. If let me treat function like "atoi" as linux c api, I can put my question in another way as : Is a document for linux c api ?

    • Admin
      Admin about 14 years
      See man <function> or info <function> and apropos <function> Where apropos gives you the manpage section to use.
  • ChristopheD
    ChristopheD about 14 years
    +1 Side note: on some platforms it's possible you'll need to install these packages (e.g. for ubuntu manpages-posix-dev (headers) and manpages-dev (functions))
  • This isn't my real name
    This isn't my real name almost 11 years
    also note that many things are defined in system-specific, (and sometimes kernel-version-specific) private header files that are #include'd by the public headers files, so searching via grep will not alway get you the correct answer.
  • haziz
    haziz about 9 years
    As noted above overly complicated.