Header file order [duplicate]
Possible Duplicate:
C++ Header order
// Here is module This.cpp
#include <boos开发者_如何学运维t/regex.hpp>
#include <iostream>
#include <math.h>
#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"
#include <list>
#include <string>
#include <stdio.h>
#include "stdafx.h" // for precomp
#include <tchar.h>
#include "This.h"
#include <Windows.h>
#include <Winsock2.h>
They are ordered by alphabet now.
I'm finding the best practice for ordering them. (Is there a nice formula?)How will you order these header files? Why?
Headers should be written to, as much as possible, 1) be independent of what may have already be included, and 2) not introduce problems (such as macros for common identifiers) for headers later included. When both of these are true, the order in which you include doesn't matter. If these aren't true, you should fix that in your own headers, or deal with it as necessary otherwise.
Thus, the choice is arbitrary, but it is still important that you make a choice! "The plan is nothing, but the planning is everything." Consistency leads to more readable code.
A relatively common order is standard library, system (as in OS), external libraries, and then headers for the same project as the current file – with the one exception of an implementation file including its "corresponding" header (if there is one) first, before any includes or other code. Within each group, I sort alphabetically by habit, because, again, it's completely arbitrary but some easy to use order lets me quickly read and update the list.
Applied to your list:
// first only because this is required in your precompiled header setup
#include "stdafx.h"
// it's too bad this can't really be first
// I'm guessing "This" refers to the corresponding header
#include "This.h"
// C stdlib then C++ stdlib is what I usually do,
// whether the C headers are spelled XXX.h or cXXX.
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <string>
// someone mentioned winsock2 needs to be before windows
#include <Winsock2.h>
#include <tchar.h>
#include <Windows.h>
#include <boost/regex.hpp>
#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"
The line breaks above to split into groups are deliberate. I would leave a comment on why winsock2 is given its own group, but the rest of the comments would not normally be there.
It is highly dependent on how you need them included. If none are dependent on another, you are relatively free to include using your own scheme (generally speaking, most important header files will be implicitly included if required by another).
Please note, however, that WinSock2.h must be included before Windows.h or else there is a high possibility that you will get a linker error at compile time.
Good luck!
Dennis M.
It doesn't really matter what order you put your header files. This is mostly a matter of choice. Many people put the system headers (the ones between <>) first and the private headers afterwards, but it's up to you.
Standard library, then system headers not in the standard library, then private headers. Separate each category by a blank line. Comment anything that needs explanation.
If you have dependencies, include the ones that headers depend on before the headers that depend on them (doi!).
精彩评论