In a header file functions.h, the first two statements are defining FUNCTIONS_H if not already defined. Can someone explain the reason for this action?
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void print();
int factorial(int);
int multiply(int, int);
#endif
The article you linked isn't about Makefiles at all, it's about compiling multi source file code.
Those are so called include guards. They prevent code from being included unnecessarily multiple times.
If
FUNCTIONS_His not defined then include the content of the file and define this macro. Otherwise, it's defined so file was already included.There is also
#pragma onceserving the very same purpose, which although not being in the standard, is supported by many major compilers.Consider example:
There are also two next header files -
func1.handfunc2.h. Both have#include "functions.h"inside.If in
main.cppwe do:The code will preprocessed to:
Then:
As you can see if not for include guards, the prototypes for functions would appear for second time. There could be also other crucial things, which redefinition would be causing errors.