c++ - Header file order -
possible duplicate:
c++ header order
// here module this.cpp #include <boost/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" // precomp #include <tchar.h> #include "this.h" #include <windows.h> #include <winsock2.h>
they ordered alphabet now.
i'm finding best practice ordering them. (is there nice formula?)
how order these header files? why?
headers should written to, as possible, 1) independent of may have included, , 2) not introduce problems (such macros common identifiers) headers later included. when both of these true, order in include doesn't matter. if these aren't true, should fix in own headers, or deal necessary otherwise.
thus, choice arbitrary, still important make choice! "the plan nothing, planning everything." consistency leads more readable code.
a relatively common order standard library, system (as in os), external libraries, , headers same project current file – 1 exception of implementation file including "corresponding" header (if there one) first, before includes or other code. within each group, sort alphabetically habit, because, again, it's arbitrary easy use order lets me read , update list.
applied list:
// first because required in precompiled header setup #include "stdafx.h" // it's bad can't first // i'm guessing "this" refers corresponding header #include "this.h" // c stdlib c++ stdlib do, // whether c headers spelled xxx.h or cxxx. #include <math.h> #include <stdio.h> #include <iostream> #include <list> #include <string> // mentioned winsock2 needs 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 split groups deliberate. leave comment on why winsock2 given own group, rest of comments not there.
Comments
Post a Comment