Is there an easy way to find out which header file a C function declaration is in? cd
ing 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?
Asked
Active
Viewed 1.5k times
7

Michael Mrozek
- 93,103
- 40
- 240
- 233
2 Answers
8
$ man 2 read
...
READ(2) Linux Programmer's Manual READ(2)
NAME
read - read from a file descriptor
SYNOPSIS
#include <unistd.h>
...

Ignacio Vazquez-Abrams
- 45,725
-
3Note that the
2
inman 2 read
is because this is a system call, many library functions will be in section 3 of the manual. You can useman -k
orapropos
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
orman 2 function_name
— the difference between 2 and 3 is largely historical) or other documentation (e.g. the GNU libc manual on Linux).

Gilles 'SO- stop being evil'
- 829,060
read
,write
,sleep
– Khaja Minhajuddin Feb 20 '12 at 19:15