3

Possible Duplicates:
what is the difference between #include <filename> and #include “filename”
C/C++ include file order/best practices

In what order should the include statements in a header file and source file come in C++? #include <> followed by #include "" or the otherwise?

Also, should the header file of a source file precede all include statements in source file?

Community
  • 1
  • 1
Sulla
  • 7,631
  • 9
  • 45
  • 71
  • 3
    Not a dupe at all. This asker understands the difference between `#include ` and `#include "foo"` and is asking which group of includes it is preferable to put first. – Brennan Vincent Jan 19 '11 at 07:23
  • 1
    @James They're completely different questions. This question pertains to the relevant "best practices," while the other pertains to the technical differences. – Maxpm Jan 19 '11 at 07:24
  • 1
    I asked same? question: http://stackoverflow.com/questions/2762568/c-c-include-file-order-best-practices – Anycorn Jan 19 '11 at 07:28

5 Answers5

7

I prefer to include in this order:

  • Standard libraries first.
  • Then third-party libraries.
  • Lastly, headers that I have written myself.
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

A general rule of thumb is to include headers in an order so as to maximize the chance of detecting that one of your own headers fails to itself include all that it needs. I.e. include that first. But since it's impossible to do that for all headers that you include, this is just a kind of vague guideline that doesn't hurt and might do some good.

When you have many headers, try to be a bit more systematic.

Like, group them by what they achieve (like [windows.h] followed by some MS header that requires [windows.h]), and/or alphabetically.

In the end, just don't use too much time on this. :-)

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Here we have the policy that a .c file has to include it's interface-header as the first one to detect missing stuff. – Rudi Jan 19 '11 at 07:41
1

There is no better or worse, they server different purposes. #ncude "" is supposed to be used for files in your project or direct dependencies that are not system wide installed. Where #include <> are for inludes that (eg under Linux) are located in your /usr/include or simialr folder, also called system libraries.

RedX
  • 14,749
  • 1
  • 53
  • 76
1

Just follow the project's existing conventions, if it has any for #include directives. If it doesn't, it doesn't really matter what you do as long as you're consistent.

Maxpm
  • 24,113
  • 33
  • 111
  • 170
0

This matters about as much as whether you put opening curly braces on their own line. I would suggest that you pick whichever one you like better, and be consistent.

Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54
  • it matters more than "putting curly braces on their own line" as it can possibly avoid hidden dependencies as discussed in http://stackoverflow.com/questions/2762568/c-c-include-file-order-best-practices – Sulla Jan 19 '11 at 08:52