Oracle® Solaris Studio 12.4: C User's Guide

Exit Print View

Updated: March 2015
 
 

2.16 How to Specify Include Files

To include any of the standard header files supplied with the C compilation system, use this format:

#include <stdio.h>

The angle brackets (<>) cause the preprocessor to search for the header file in the standard place for header files on your system, usually the /usr/include directory.

The format is different for header files that you have stored in your own directories:

#include "header.h"

For statements of the form #include "foo.h" (where quotation marks are used), the compiler searches for include files in the following order:

  1. The current directory (that is, the directory containing the “including” file)

  2. The directories named with -I options, if any

  3. The /usr/include directory

If your header file is not in the same directory as the source files that include it, use the -I compiler option to specify the path of the directory in which it is stored. For instance, suppose you have included both stdio.h and header.h in the source file mycode.c:

#include <stdio.h>
#include "header.h"

Suppose further that header.h is stored in the directory../defs. You might then want to use this command:

% cc  –I../defs  mycode.c

It directs the preprocessor to search for header.h first in the directory containing mycode.c, then in the directory ../defs, and finally in the standard place. It also directs the preprocessor to search for stdio.h first in ../defs, then in the standard place. The difference is that the current directory is searched only for header files whose names you have enclosed in quotation marks.

You can specify the– I option more than once on the cc command-line. The preprocessor searches the specified directories in the order they appear. You can specify multiple options to cc on the same command line:

% cc– o prog– I../defs mycode.c