Google C++ Style Guide include order
Asked Answered
Z

2

4

Google C++ Style Guide recommends to include the header files (.h) to the implementation files (.cpp, .cc) in the following order:

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

dir2/foo2.h.
A blank line
C system files.
C++ system files.
A blank line
Other libraries' .h files.
Your project's .h files.

As said such order allows to see the omitted dependencies in dir2/foo2.h while compiling the foo-unit, rather than other innocent units. Seems quite logically.

But why Other libraries' .h files. and Your project's .h files. placed to the end of the list? In theory there also could be missing dependencies which will be hidden by including the C system files. and C++ system files. before.

Maybe there is assumed that other (header) files problems should be detected in related implementation files? What about header-only libraries in that case?

In other words, would the following order of includes:

dir2/foo2.h.
A blank line
Other libraries' .h files.
Your project's .h files.
A blank line
C system files.
C++ system files.

will be better to more fast finding hidden dependencies?

For instance, if I have only header that requires <stdio.h> (but not specified in that file). Using the Google order the probabily of direct or indirect include of <stdio.h> is higher, than when the system files are included on the last step (as I suggested before). Hence, low probability to find hidden dependency. So what we will win if include the system files before other lib/your project files?

Also it's not clear, should I use recommended order of include files in other (user defined) header files.

Zitvaa answered 24/1, 2019 at 13:29 Comment(2)
Those missing dependencies should come up when building their respective source files, if the same guide-lines are followed for that.Vladimir
@Someprogrammerdude please see post update.Papa
F
3

Every header file(h, hpp,...) should have an implementation file (cpp, cc,...) where the order of including is the same as specified in the question. (Even if the implentation file is empty, except for this 1 include)

So just like "Your" header is included first in "Your" implementation file, so every "other" header file should be included first in the "other" implementation file.

This way if the "other" header does not include a required header, the "other" implementation file will not compile.

enter link description here

Flavouring answered 24/1, 2019 at 14:18 Comment(3)
So ... why the order when system files are included before other/your .h files is better than opposite?Papa
I haven't seen any argument why this is better "than opposite". Many rules in the Google Style Guide were written without any justification. The rule about first including "Your" header file was added in the last couple of years. So maybe your argument is in fact better. Or maybe it doesn't matter? If every header file in your project abides by this rule, doesn't the order of the next header files not matter? Maybe it's just a rule, so that all projects are consistant.Flavouring
The Bloomberg Coding Standards says: Include directives must be divided into three groups: (a) components in the same package as this component (b) components from other packages (c) other required headers that are not components (e.g. third-party or platform-specific). Which would support your argument.Flavouring
P
-1

The C and C++ header files to include there are those used by the source code below, they should not cover dependencies of other libraries or project's header files.

And header only modules like templates: You do write a test program for your modules, right? Include the template header file first in the test program, then you can detect missing includes there too.

Pathy answered 24/1, 2019 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.