Is there an easy way to find out which header file a C function declaration is in? cding into /usr/include and running (grep -E 'system.*\(' *.h -R) works with some trial and error, but isn't there an easier way to do this?

$ man 2 read


READ(2)                    Linux Programmer's Manual                   READ(2)

       read - read from a file descriptor

       #include <unistd.h>
  • 3
    Note that the 2 in man 2 read is because this is a system call, many library functions will be in section 3 of the manual. You can use man -k or apropos to get a list of commands/calls/files/APIs whose name or description includes a word and what section(s) they are described in. Feb 20 '12 at 23:00

If you search in the header files on your system, you'll find in what header file the function is declared on your system. It may be difficult as sometimes the function is declared via a complex macro. And it'll only tell you what header file declares the function on your system; this may even be a header file that you can't include directly because it needs other headers to come first.

The only safe way to find what header file to include is to consult the documentation of the library that provides the function. For the standard library, you can consult:

  • The C standard (C89=C90, C99 or C11) defines functions that are available on every platform running C.
  • The POSIX/Single Unix standard (POSIX:2004, POSIX:2008) defines functions that are available on every unix-like platform.
  • Your platform may have other functions or may offer additional features. Consult its man pages (man 3 function_name or man 2 function_name — the difference between 2 and 3 is largely historical) or other documentation (e.g. the GNU libc manual on Linux).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.