Include directives
The files that are included from the ‘include’ directory of the C/C++ program development software are enclosed between angle brackets angle brackets (< >):
#include <stdlib.h> // For standard C library fn’s
Header files included from the same directory or any other directory in the path are enclosed between double quotation marks (“ “):
#include “motor.h” // For motor class objects used.
The proper use of angle brackets and double quotation marks is crucial as it allows the compiler to efficiently locate the included header files during compilation and linking.
Preventing Multiple Inclusions of Header Files
A particular header file is included only once in a program. Should a header file be included more than once, the compiler will interpret this as an error and issue an error message. The compiler can interpret multiple inclusion as, for example, the redefinition of a class that is already defined by the first instance of the include file
– hence the error!
The general rule “Include the header file of the object you use in the program” may lead to multiple inclusions. For example, if a program uses a DCMotor object and a StepperMotor object, we must include dcmotor.h as well as stepper.h files. They both have motor.h included in them. As a result motor.h will be included twice. There is a mechanism that enables us to practice the general rule above and at the same time avoid multiple inclusions of the same header file. The procedure uses a ‘status flag’, explained as follows:
Before the preprocessor begins to include header files, the flag will be inactive (file not included).
The first time a particular header file is presented for inclusion, the flag will be tested, and the result will indicate that the header file has not been included. The header file will be included this time and the flag will then be activated, indicating inclusion has now taken place.
When this header file is presented for inclusion on subsequent occasions, the flag will be tested and its status (this time; file included) will direct the preprocessor to ignore this file, preventing any multiple inclusions.
Sentries for header files
Sentries in header files are compiler directives for the preprocessor. They implement the function of the ‘status flag’ just described and prevent the compiler from including the same header file more than once.
The following example uses the AbstactMotor class to show how sentries are added to a header file.