Naming Include Guards

I personally follow Boost’s recommendation. It’s perhaps one of the largest collection of C++ libraries of good quality around and they don’t have problem. It goes like: <project>_<path_part1>_…_<path_partN>_<file>_<extension>_INCLUDED // include/pet/project/file.hpp #ifndef PET_PROJECT_FILE_HPP_INCLUDED which is: legal (note that beginning by _[A-Z] or containing __ is not) easy to generate guaranteed to be unique (as a include … Read more

Purpose of Header guards

The guard header (or more conventionally “include guard”) is to prevent problems if header file is included more than once; e.g. #ifndef MARKER #define MARKER // declarations #endif The first time this file is #include-ed, the MARKER preprocessor symbol will be undefined, so the preprocessor will define the symbol, and the following declarations will included … Read more

C++ #include guards

The preprocessor is a program that takes your program, makes some changes (for example include files (#include), macro expansion (#define), and basically everything that starts with #) and gives the “clean” result to the compiler. The preprocessor works like this when it sees #include: When you write: #include “some_file” The contents of some_file almost literally … Read more

Why aren’t my include guards preventing recursive inclusion and multiple symbol definitions?

FIRST QUESTION: Why aren’t include guards protecting my header files from mutual, recursive inclusion? They are. What they are not helping with is dependencies between the definitions of data structures in mutually-including headers. To see what this means, let’s start with a basic scenario and see why include guards do help with mutual inclusions. Suppose … Read more

tech