Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Multiple include guards may be useful for" what, exactly?

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.)

like image 238
R.M. Avatar asked Nov 17 '15 22:11

R.M.


People also ask

Should you always use include guards?

Always write internal #include guards. Never write external #include guards.

How important is it to have an include guard for every header file?

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.


1 Answers

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++.

like image 90
Tom Tromey Avatar answered Sep 20 '22 02:09

Tom Tromey