开发者

What is the better way of including a header file? #include<> followed by #include"" or the otherwise? [duplicate]

开发者 https://www.devze.com 2023-02-05 05:08 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: what is the difference between #include <filename> and #include “filename”
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

what is the difference between #include <filename> and #include “filename”

C/C++ include file order/best practices

In what order sh开发者_C百科ould 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?


I prefer to include in this order:

  • Standard libraries first.
  • Then third-party libraries.
  • Lastly, headers that I have written myself.


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.,


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消