# C/C++ Include Guidelines

DevFeed: [C/C++ Include Guidelines](<https://devfeed.tech/articles/c-c-include-guidelines-28619.md>)

Original publisher: [Read original article](<https://upcoder.com/20/cc-include-guidelines>)

Published: 2019-10-13T12:14:00Z

Content type: tutorial

Language: en

Sources: [Thomas Young](<https://devfeed.tech/sources/thomas-young.md>)

Topics: [c/c++](<https://devfeed.tech/topics/c-c-plus-plus.md>), [C](<https://devfeed.tech/topics/c.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Code quality](<https://devfeed.tech/topics/code-quality.md>), [Code](<https://devfeed.tech/topics/code.md>), [modules](<https://devfeed.tech/topics/modules.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [c-c-plus-plus](<https://devfeed.tech/tags/c-c-plus-plus.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [code-quality](<https://devfeed.tech/tags/code-quality.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [modules](<https://devfeed.tech/tags/modules.md>), [source](<https://devfeed.tech/tags/source.md>), [structure](<https://devfeed.tech/tags/structure.md>)

## AI overview

An opinionated set of guidelines for organizing include files in C and C++. It covers header and source-file responsibilities, include ordering, repeated-include protection, dependencies, namespaces, naming, and related practices, while noting that C++20 modules change the situation.

## Source excerpt

Some (opinionated) guidelines for include file organisation in C/C++. Intro I present a list of points, probably best considered as guidelines, but which I'll refer to as rules throughout the rest of this post, since it trips off the tongue that bit more easily (or perhaps I just have some hidden authoritarian streak). I'll assume the reader knows something about writing code in C or C++, and understands at least the basics about how compilation with include files works. Sometimes I'll say things are obvious to this intended audience, where I think this is necessary for completeness. This is about an ideal way to organise code. For large existing code bases changing the code to follow all of the rules may not be straightforward but it should be possible to make some incremental steps towards the code structure I describe, resulting in some kind of incremental improvements in overall code quality. Many of the rules have exceptions, and it can interesting to explore reasons for these exceptions, as well as the reasons for the rules themselves. Modules As an aside, please note that this is a 'pre-modules' document. If you're using c++ 20 and modules the situation will be different. (At the time of writing I don't have any experience working with modules, and I'm not aware of all the details about how modules will work. I'm hoping that if we follow the rules described here, we should be reasonably well placed for moving to C++20 modules, but if that's not the case, and there are module related details that you think should be taken into account in this document, please let me know in the comments.) The rules 1. Headers are for linkage details, implementation goes in source files. 2. Each source file should have a single matching header. 3. Linkage to a source file should go only through its header. 4. Guard against repeated includes. 5. Each header should be self-sufficient. 6. Each source file includes its own header first. 7. Low level includes come after high level i