ILE C/C++ Language Reference

The #include directive

A preprocessor include directive causes the preprocessor to replace the directive with the contents of the specified file.

Read syntax diagramSkip visual syntax diagram#include directive syntax
 
>>-#--include--+-"--+-----------+--file_name--"-+--------------><
               |    '-file_path-'               |
               '-<--+-----------+--file_name-->-'
                    '-file_path-'
 

Using the #include Directive when Compiling Source in a Data Management File

The following table indicates the search path the compiler takes for source physical files. See the default file names and search paths below.

Table 24. Search Path Compiler for Source Physical Files
Filename Member File Library
mbr mbr default file default search
file/mbr1 mbr file default search
mbr.file mbr file default search
lib/file/mbr mbr file lib
lib/file(mbr) mbr file lib
Note: 1 If the include file format <file/mbr.h> is used, the compiler searches for mbr in the file in the library list first. If mbr is not found, then the compiler searches for mbr.h in the same file in the library list. Only "h" or "H" are allowed as member name extensions.

If library and file are not specified, the preprocessor uses a specific search path depending on which delimiter surrounds the filename. The < > delimeter specifies the name as a system include file. The " " delimiter specifies the name as a user include file.

The following describes the search paths for the #include directive used by the compiler.

User includes are treated the same as system includes when the *SYSINCPATH option has been specified with the Create Module or Create Bound Program commands.

The preprocessor resolves macros on a #include directive. After macro replacement, the resulting token sequence must consist of a file name enclosed in either double quotation marks or the characters < and >. For example:

#define MONTH <july.h>
#include MONTH

Usage

If there are a number of declarations used by several files, you can place all these definitions in one file and #include that file in each file that uses the definitions. For example, the following file defs.h contains several definitions and an inclusion of an additional file of declarations:

/* defs.h */
#define TRUE 1
#define FALSE 0
#define BUFFERSIZE 512
#define MAX_ROW 66
#define MAX_COLUMN 80
int hour;
int min;
int sec;
#include "mydefs.h"

You can imbed the definitions that appear in defs.h with the following directive:

#include "defs.h"

One of the ways you can combine the use of preprocessor directives is demonstrated in the following example. A #define is used to define a macro that represents the name of the C or C++ standard I/O header file. A #include is then used to make the header file available to the C or C++ program.

#define IO_HEADER   <stdio.h>
      .
      .
      .
#include IO_HEADER   /* equivalent to specifying #include <stdio.h>  */
      .
      .
      .

Using the #include Directive When Compiling Source in an Integrated File System File

You can use the SRCSTMF keyword to specify an Integrated File System file at compile time. The #include processing differs from source physical file processing in that the library list is not searched. The search path specified by the INCLUDE environment variable (if it is defined), and the compiler’s default search path are used to resolve header files.

The compiler’s default include path is /QIBM/include.

#include files use the delimeters " " or <>.

When attempting to open the include file, the compiler searches in turn each directory in the search path until the file is found or all search directories have been exhausted.

The algorithm to search for include files is:

if file is fully qualified (a slash / starts the name) then
  attempt to open the fully qualified file
else
  if "" is delimeter, check job's current directory
  if not found:
     loop through the list of directories specified in the INCLUDE
          environment variable and then the default include path
     until the file is found or the end of the include path is encountered
  endif

For more information, refer to Using the ILE C/C++ Stream Functions with the IBM® i Integrated File System in Websphere Development Studio: ILE C/C++ Programmer’s Guide .



[ Top of Page | Previous Page | Next Page | Contents | Index ]