Why Are #Ifndef and #Define Used in C++ Header Files

Why are #ifndef and #define used in C++ header files?

Those are called #include guards.

Once the header is included, it checks if a unique value (in this case HEADERFILE_H) is defined. Then if it's not defined, it defines it and continues to the rest of the page.

When the code is included again, the first ifndef fails, resulting in a blank file.

That prevents double declaration of any identifiers such as types, enums and static variables.

Why use #ifndef CLASS_H and #define CLASS_H in .h file but not in .cpp?

First, to address your first inquiry:

When you see this in .h file:

#ifndef FILE_H
#define FILE_H

/* ... Declarations etc here ... */

#endif

This is a preprocessor technique of preventing a header file from being included multiple times, which can be problematic for various reasons. During compilation of your project, each .cpp file (usually) is compiled. In simple terms, this means the compiler will take your .cpp file, open any files #included by it, concatenate them all into one massive text file, and then perform syntax analysis and finally it will convert it to some intermediate code, optimize/perform other tasks, and finally generate the assembly output for the target architecture. Because of this, if a file is #included multiple times under one .cpp file, the compiler will append its file contents twice, so if there are definitions within that file, you will get a compiler error telling you that you redefined a variable. When the file is processed by the preprocessor step in the compilation process, the first time its contents are reached the first two lines will check if FILE_H has been defined for the preprocessor. If not, it will define FILE_H and continue processing the code between it and the #endif directive. The next time that file's contents are seen by the preprocessor, the check against FILE_H will be false, so it will immediately scan down to the #endif and continue after it. This prevents redefinition errors.

And to address your second concern:

In C++ programming as a general practice we separate development into two file types. One is with an extension of .h and we call this a "header file." They usually provide a declaration of functions, classes, structs, global variables, typedefs, preprocessing macros and definitions, etc. Basically, they just provide you with information about your code. Then we have the .cpp extension which we call a "code file." This will provide definitions for those functions, class members, any struct members that need definitions, global variables, etc. So the .h file declares code, and the .cpp file implements that declaration. For this reason, we generally during compilation compile each .cpp file into an object and then link those objects (because you almost never see one .cpp file include another .cpp file).

How these externals are resolved is a job for the linker. When your compiler processes main.cpp, it gets declarations for the code in class.cpp by including class.h. It only needs to know what these functions or variables look like (which is what a declaration gives you). So it compiles your main.cpp file into some object file (call it main.obj). Similarly, class.cpp is compiled into a class.obj file. To produce the final executable, a linker is invoked to link those two object files together. For any unresolved external variables or functions, the compiler will place a stub where the access happens. The linker will then take this stub and look for the code or variable in another listed object file, and if it's found, it combines the code from the two object files into an output file and replaces the stub with the final location of the function or variable. This way, your code in main.cpp can call functions and use variables in class.cpp IF AND ONLY IF THEY ARE DECLARED IN class.h.

I hope this was helpful.

Why do Eclipse-generated header files start with #ifndef and #define?

Yes, this is very similar to PHP #INCLUDE_ONCE. The idea is that a header files contains declarations for functions and variables that are used in other modules. Those modules must #include the header file to be able to access those functions and variables. But if a source file #include's a header and then #include's another header that also #include's that header, you get two copies of those declarations, which causes all sorts of problems.

This common technique prevents duplicate inclusion. It insures that any code unit that #include's a header gets a copy of it the first time it is requested, but not again.

C++ #ifndef for include files, why is all caps used for the header file?

These are preprocessor symbols and have no such rules. (as long as they match the #defines in the headers)

However, convention is to use all-caps for preprocessor symbols.

Why is #endif included at the end of the .h page?

You need to exclude the full contents of the file if it's already processed.

The purpose is not to #define the header guard - that's a useful(ish) side effect. The purpose is to ensure the header content is loading only once.

Multiple #ifndef statements - which one gets applied

Each file is compiled separately. Macros from one file are not visible in any other file. Once the files are independently compiled, the resulting objects are linked together to create an executable.

C header file #ifndef #include error

You have a circular dependency. Header file headerA.h depends on headerB.h which depends on headerA.h and so on and on.

You need to break that dependency, for example by not including headerB.h in headerA.h. It's not needed (nothing in headerA.h needs anything from headerB.h).


If you have to include headerB.h (as stated in your recent edit) then you first should reconsider how you use your header files, and what definition you place where. Perhaps move the definition of MyInt to headerB.h? Or have more header files, like one for type-aliases (like your MyInt which I personally see no use for), one for structures and one for variable declarations?

If that's not possible then you could try by changing the order of definitions and the include, like

#ifndef HEADERA_H
#define HEADERA_H

// Define type alias first, and other things needed by headerB.h
typedef int MyInt;

// Then include the header file needing the above definitions
#include "headerB.h"

TFoo foo;
... some other structures from headerB.h ...

#endif

In C and C++, why is each .h file usually surrounded with #ifndef #define #endif directives?

It's a so-called "include guard". The purpose is to prevent the file from having to be parsed multiple times if it is included multiple times.



Related Topics



Leave a reply



Submit