In one large C/C++ project I worked on #include files were so haphazard and complex that Every now and then an ambitious soul would set out to fix the problem once and for all. The code in this project was quite good – it’s just maintaining good include The best weapon is being anal-retentive from the very beginning of your project. It’s important to reduce number of dependencies between header files, WebKit project spells the rules:
Node.cpp should include Node.h first, before other files. This guarantees that each header’s completeness is tested, to make sure it can be compiled without requiring any other header files be included first.
command-line sort tool or the Xcode sort selection command). Don’t bother to organize them in a logical order. File config.h #ifndef COMMON_H_ #define COMMON_H_ /* This is a header included by every single file. It should include definitions of most basic types and utilities (like asserts and simple templates. It usually means most common header files for C standard library like <stdio.h> etc. and <windows.h> on Windows. Resist temptation to include too much in this file - it'll lead to insanity. */ #endif File foo.h: #ifndef FOO_H_ #define FOO_H_ /* To speedup compilation times avoid #include'ing header files. You can use any definitions in config.h. If possible, use forward declarations instead of including header file with definitions */ #endif File foo.c: #include "config.h" #include "foo.h" /* rest of include files sorted alphabetically */ |