Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Header file inclusion order for C++

📅 2010-Jul-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

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>