Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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: 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 Answers 2

up vote 8 down vote accepted
$ man 2 read

...

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

NAME
       read - read from a file descriptor

SYNOPSIS
       #include <unistd.h>
 ...
share|improve this answer
    
man is awesome. Thank you. –  Khaja Minhajuddin Feb 20 '12 at 19:22
2  
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 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).
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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