C++ Header order [closed]

2019-01-05 10:12发布

What order should headers be declared in a header / cpp file? Obviously those that are required by subsequent headers should be earlier and class specific headers should be in cpp scope not header scope, but is there a set order convention / best practice?

10条回答
男人必须洒脱
2楼-- · 2019-01-05 10:33

In a header file you have to include ALL the headers to make it compilable. And don't forget to use forward declarations instead of some headers.

In a source file:

  • corresponded header file
  • necessary project headers
  • 3rd party libraries headers
  • standard libraries headers
  • system headers

In that order you will not miss any of your header files that forgot to include libraries by their own.

查看更多
放我归山
3楼-- · 2019-01-05 10:40

In header files, I tend to put standard headers first, then my own headers (both lists being ordered alphabetically). In implementation files, I put first the header corresponding (if any), then standards headers and other dependency headers.

Order is of little importance, except if you make a great use of macros and #define ; in that case, you must checked that a macro you defined doesn't replace a previously included one (except if that's what you want, of course).

Concerning this statement

those that are required by subsequent headers should be earlier

A header shouldn't rely on other headers being included before it! If it requires headers, it just includes them. Header guards will prevent multiple inclusion:

#ifndef FOO_HEADER_H
#define FOO_HEADER_H
...
#endif

EDIT

Since I wrote this answer, I changed my way of ordering the include directives in my code. Now, I try to always put headers in increasing order of standardization, so the headers of my project come first, followed by 3rd party libraries headers, followed by standard headers.

For instance, if one of my file uses a library I wrote, Qt, Boost and the standard library, I will order the includes as follow:

//foo.cpp
#include "foo.hpp"

#include <my_library.hpp>
// other headers related to my_library

#include <QtCore/qalgorithms.h>
// other Qt headers

#include <boost/format.hpp> // Boost is arguably more standard than Qt
// other boost headers

#include <algorithms>
// other standard algorithms

The reason why I do that is to detect missing dependencies in my own headers: let's assume for instance that my_library.hpp uses std::copy, but doesn't include <algorithm>. If I include it after <algorithm> in foo.cpp, this missing dependency will go unnoticed. On the contrary, with the order I just presented, the compiler will complain that std::copy has not been declared, allowing me to correct my_library.hpp.

In each "library" group, I try to keep the include directives ordered alphabetically, to find them more easily.

On a sidenote, a good practice is also to limit at a maximum the dependency between header files. Files should include as little headers as possible, especially headers file. Indeed, the more headers you include, the more code needs to be recompiled when something changes. A good way to limit these dependencies is to use forward declaration, which is very often sufficient in header files (see When can I use a forward declaration?).

查看更多
不美不萌又怎样
4楼-- · 2019-01-05 10:41

Google C++ Style Guide, Names and Order of Includes :

In dir/foo.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:

  • dir2/foo2.h (preferred location — see details below).
  • C system files.
  • C++ system files.
  • Other libraries' .h files.
  • Your project's .h files.
查看更多
孤傲高冷的网名
5楼-- · 2019-01-05 10:45

For .cpp files, you should include the header of the class or whatever you are implementing first, so you catch the case where this header is missing some includes. After that, most coding guidelines tend to include system headers first, project headers second, for example the Google C++ Style Guide.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-05 10:46

Good practice: every .h file should have a .cpp that includes that .h first before anything else. This proves that any .h file can be put first.

Even if the header requires no implementation, you make a .cpp that just includes that .h file and nothing else.

This then means that you can answer your question any way you like. It doesn't matter what order you include them in.

For further great tips, try this book: Large-Scale C++ Software Design - it's a shame it's so expensive, but it is practically a survival guide for C++ source code layout.

查看更多
The star\"
7楼-- · 2019-01-05 10:48

If a header needs other headers then it just includes them in that header.

Try to structure your code so you pass pointers or references and forward declare where you can.

In the implementation then the header that defines it should be listed first (except in Visual Studio if you are using pch then stdafx would go first).

I generally list them as I need.

查看更多
登录 后发表回答