When I create header file in Microsoft Visual Studio, it’s just a blank file, and I often add some prototype of function or class etc…
When I create header file from Netbean. It’s often has this structure. (Assume my header file is example):
#ifndef EXAMPLE_H
#define EXAMPLE_H
// put code here
#endif
Please tell me, meaning of those bunch of codes above. If I delete this, are there any problem ? I feel strange because VS doesn’t generate this.
Thanks 🙂
These are called include guards, they prevent multiple definition errors when you include the same file multiple times. Even if you don’t, they don’t hurt anybody, so you might as well leave them in.
How it works is, it checks if the macro
EXAMPLE_His defined. If it is, it leaves out the entire file. If it isn’t, it defines it, and keeps the file. That way, the next time this file appears the contents are left out.This often happens if you have two different headers that include the same header, such as:
a.h
b.h
c.h
And then you create stuff.cpp with
after preprocessing stuff.cpp will have
as opposed to (without include guards)
which will give you an error about multiple definitions of
Foo.