Specifying path names for include files
When you imbed one source file in another using the #include preprocessor
directive, you must supply the name of the file to be included. You can specify
a file name either by using a full path name or by using a relative path name.
- Use a full path name to imbed files
The full path name, also called the absolute path name, is the file's complete name starting from the root directory. These
path names start with the / (slash) character. The full
path name locates the specified file regardless of the directory you are presently
in (called your working or current directory).
The following example specifies the full path to
file mine.h in John Doe's subdirectory example_prog:
/u/johndoe/example_prog/mine.h
- Use a relative path name to imbed files
The relative path name locates a file relative to the directory
that holds the current source file or relative to directories defined using
the -Idirectory option.
Directory search sequence for include files using relative path names
C and C++ define two versions
of the #include preprocessor directive. IBM XL C/C++ supports
both. With the #include directive, the include file name is enclosed
between either the < > or " " delimiter
characters.
Your choice of delimiter characters will determine the search path used
to locate a given include file name. The compiler will search for that include
file in all directories in the search path until the include file is found,
as follows:
#include type |
Directory search order |
#include <file_name> |
- The compiler first searches for file_name in each
user directory specified by the -Idirectory compiler option, in the order that they appear on the command
line.
- For C++ compilations, the compiler then searches
the directories specified by the -qcpp_stdinc and -qgcc_cpp_stdinc compiler options.
- Finally,
the compiler then searches the directories specified by the -qc_stdinc and -qgcc_c_stdinc compiler options.
|
#include "file_name" |
- The compiler first searches for the include file in the directory where
your current source file resides. The current source file is the file that
contains the directive #include "file_name".
- The compiler then searches for the include file according to the search
order described above for #include <file_name>.
|
Notes:
- file_name specifies the name of the file to be
included, and can include a full or partial directory path to that file if
you desire.
- If you specify a file name by itself, the compiler searches for the file
in the directory search list.
- If you specify a file name together with a partial directory path, the
compiler appends the partial path to each directory in the search path, and
tries to find the file in the completed directory path.
- If you specify a full path name, the two versions of the #include directive have the same effect because the location of the file to
be included is completely specified.
- The only difference between the two versions of the #include directive
is that the " " (user include) version first begins
a search from the directory where your current source file resides. Typically,
standard header files are included using the < > (system include)
version, and header files that you create are included using the " " (user include) version.
- You can change the search order by specifying the -qstdinc and -qidirfirst options along with the -Idirectory option.
Use the -qnostdinc option to search only the directories specified with the -Idirectory option and the current
source file directory, if applicable.
Use the -qidirfirst option
with the #include "file_name" directive to search the directories specified with the -Idirectory option before searching other directories.
Use the -I option to specify the directory search
paths.
Related information