I've been playing around with the -H option of gcc, which prints out information about direct and indirect includes in C and C++ compilation (relevant section of the gcc manual).
As part of the output there's a section "Multiple include guards may be useful for:", which lists a number of project and system headers.
What does this mean, how is this list determined, and why is it saying "Multiple" include guards may be useful?
(I'm familiar with the general concept of include guards, and why one would want to use them - I'm looking for details about this message in particular, and what it means for my project.)
Always write internal #include guards. Never write external #include guards.
Without a header guard, a code file could end up with multiple (identical) copies of a given type definition, which the compiler will flag as an error.
In this case, "multiple" modifies "includes" -- not "guards". So "include guards" and "multiple include guards" are the same thing. That is, they guard against multiple includes of the same file.
The list is made by something approximately like this: first, all files are considered for the list. However, the main file is excluded; and files that have an include guard; files that are opened with #import
; and files that have #pragma once
.
Reasons to use guards are that they make a header file idempotent, and they can reduce compilation time. IMO they are a best practice for C and C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With