7

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?

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • Have you tried consulting the documentation yet? – Ignacio Vazquez-Abrams Feb 20 '12 at 19:04
  • @IgnacioVazquez-Abrams Thanks for mentioning that. I was able to find the system() call in the docs (I am using this: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf). However, there are many other functions which seem to be missing in the docs, e.g. read, write, sleep – Khaja Minhajuddin Feb 20 '12 at 19:15

2 Answers2

8
$ man 2 read

...

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

NAME
       read - read from a file descriptor

SYNOPSIS
       #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. – dmckee --- ex-moderator kitten Feb 20 '12 at 23:00
1

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).