Programming Techniques:C/CPP Header File Convention

From GPWiki
Jump to: navigation, search

Contents

C/C++ Header File Convention

At some point, a person learning to program in C/C++ learns the distinction between "header" files and "source" files. That is, a source file is compiled by the compiler and header files are shared among the source files. Then the question becomes, "How do I use header files effectively?" This is a description of the modern convention for what goes into header files, how header files are organized, and how they are used.

A Short History of Header Files

In the Middle Ages of the Computer Era (that would be the 1980s), the "header" file was born, and it was used to store all the constants and declarations that a "source" file would need. It included internal details and references to external functions (using the evil "extern" keyword). Header files were regarded as the file equivalent of a macro and they were used primarily for convenience.

A typical header file for main.c looked something like this:

/* main.h (included by main.c) circa 1980. Don't do this. */
 
/* Header files needed by main.c */
 
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
 
/* External data referenced by main.c */
 
extern char z[128];
 
/* Functions in foo.c that are called by main.c */
 
extern int a( char * );
extern int b( char * );
 
/* Functions defined and used only in main.c */
 
static int x();
static int y();
 
/* Constants used by main.c */
 
#define X		1
#define Y		2

With the birth of Object-Oriented Programming, it was realized that the way header files were being used was backwards, and that it is much better to use a header file as a description of the API for a source file or module. The declarations in the header file describe what constants, types, data, and functions the source file or module provides to its users. This is the convention in common use today.

What Goes in a Header File

As stated above, a header file is used to describe the API for a module or source file. It contains declarations of all the constants, types, data, and functions provided by the module.

Here are the things that go in a header file:

What Does Not Go in a Header File

In the current convention, exposing internal or private constants, types, data, and functions to the world is wrong. None of the lines in the code example above should be included in a header file.

Here is what does not go in a header file for a module:


Including Header Files In A Source File

The order of inclusion of header files is mostly a matter of preference, with the exception of one requirement and one very useful convention. Here is the preferred order:

Here's an example of including header files in a source file:

/* controller.c */
 
#include "precompiledheader.h"
 
/* This file’s header file */
#include "controller.h"
 
/* Header files in this file’s module */
#include "gamemenu.h"
#include "menus.h"
#include "preferences.h"
 
/* Header files in sibling or parent modules */
#include "../UserInput/userinput.h"
#include "../UserInput/vibration.h"
 
/* Header files in unrelated modules */
#include "Zap/button.h"
#include "Zap/cycler.h"
#include "Zap/dialog.h"
#include "Zap/zap.h"
 
/* Standard library header files */
#include <stdio.h>
 
...
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox