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

Summary

Wear head(er) protection: Prevent unintended multiple inclusions by using #include guards with unique names for all of your header files.

Discussion

Each header file should be guarded by an internal #include guard to avoid redefinitions in case it is included multiple times. For example, a header file foo.h should follow the general form:

image

Observe the following rules when defining include guards:

Use a unique guard name: Make sure it is unique at least within your application. We used a popular convention above; the guard name can include the application ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.