Include guards
From Cppwiki
Oh no! My code doesn't work! I get an error about multiple class definitions! Here's what my header, myclass.h, looks like:
// Here is my class, which does things. class MyClass { void DoThings(); };
The problem occurs when MyClass is defined twice in a single translation unit, which generally happens when myclass.h is included twice. You don't necessarily need #include "myclass.h" twice in a single source file for this to occur - imagine a case where main.cpp includes a.h and b.h, both of which include myclass.h.
The solution, luckily, is easy.
#ifndef MYPROJECT_MYCLASS #define MYPROJECT_MYCLASS // Here is my class, which does things. class MyClass { void DoThings(); }; #endif
Every time it includes the file, it checks to see if MYPROJECT_MYCLASS is defined. If it isn't, it defines MYPROJECT_MYCLASS - thus ensuring the body won't be included a second time - and then includes the body. If it is, it just skips the rest of the file (down to the #endif).
MYPROJECT_MYCLASS must be unique, otherwise you'll have bizarre issues with files not being included properly. How you guarantee it's unique is up to you - some people include their name, some people include a file path, some people include a timestamp or a random number. (E.g. AUTHOR_YYYYMMDD_HHMM contains some possibly useful information, isn't too long, and - most importantly - has a great deal of permanent global uniqueness.) Often, people start it with an underscore - however, this is incorrect and shouldn't be done, as those are reserved names.
#pragma once
| WARNING: | Non-Standard Feature | This section includes information about a non-standard language extension |
|---|
"#pragma once" or a similar mechanism, when offered by a compiler, provides the following minor advantages to include guards:
- Reduces the possibility of various errors (e.g. #ifdef instead of #ifndef, forgetting the closing #endif, dependance of the guard name on the header name, repeating the same unique identifier twice)
- Does not pollute the global namespace with a macro
- Is slightly more likely to trigger a mechanism within the compiler which will speed up compilation by skipping over the header if it is included s second time
- Is slightly faster to type
It also has the following disadvantages:
- Is not standard C++
- Is not guaranteed to exist on all compilers
- Is not nearly as well-known or as common as include guards
The channel ops have mixed opinions regarding the use of "#pragma once".
I'm not sure exactly where #pragma once started, it's a historical detail I've never really looked into, so I've always kind of filled in the blanks. I've often thought it was a Microsoft invention, way back in the day of MSC (version 3, I think), and so I've always taken the same tact to using it that MS uses in their own headers. Nominally, they acknowledge that it's a non-standard, non-portable feature, and take steps to insure that it's only invoked when run through their own compiler, by wrapping it in a unique set of preprocessing checks. Regardless, they still use classic inclusion guards on all of their headers.
The only major benefit to #pragma once is that, for compilers that support it, and don't already do exclusive-multiple-inclusion avoidance, is that it lets the preprocessor know not to recurse the file again, at all. In my opinion, the speed gains are minimal, giving it limited advantage over the alternative. Since it's non-portable, and non-standard, it's also not a replacement. Even if you choose to use #pragma once, your headers should still be wrapped with inclusion guards.