Header file inclusion order for C++

Once the C++ code of a project reaches a certain size or maturity, good coding conventions become crucial to have manageable code. One such convention is the header file inclusion order, i.e., in which order to list the header files that you include for the file.

Every programmer seems to agree that some order is needed, but no one seems to agree on one. Programmers are divided between the order going from local to global or vice versa. The Google C++ Style Guide gives an arbitrary order without explaining why it helps!

I like the order going from local to global:

// Source file of car.cpp

// Self
#include "car.h"

// Project
#include "engine.h"
#include "steering.h"
#include "wheel.h"

// External libs
#include <OpenGL.h>
#include <WxWidgets.h>

// C++ std libs
#include <cassert>
#include <memory>
#include <vector>

// C std libs
#include <math.h>
#include <stdlib.h>
Advertisements

One thought on “Header file inclusion order for C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s