Is there an easy way to find out which header file a C function declaration is in?
/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?
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_nameor
man 2 function_name— the difference between 2 and 3 is largely historical) or other documentation (e.g. the GNU libc manual on Linux).