Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 1 | <!-- go/cmark --> |
Danil Chapovalov | 787b907 | 2024-09-02 11:48:33 | [diff] [blame] | 2 | <!--* freshness: {owner: 'danilchap' reviewed: '2024-09-02'} *--> |
Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 3 | |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 4 | # `.h` and `.cc` files come in pairs |
| 5 | |
Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 6 | This is an overflow page for [this](/g3doc/style-guide.md#h-cc-pairs) |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 7 | style rule. |
| 8 | |
| 9 | ## Example violations |
| 10 | |
| 11 | Example violations, which should be avoided in new code: |
| 12 | |
| 13 | * Declarations in `path/to/include/foo.h`, definitions in |
| 14 | `path/to/source/foo.cc`. **Fix:** The `.h` and `.cc` files should be |
| 15 | in the same directory. |
| 16 | * Declarations in `foo.h`, definitions in both `foo_bar.cc` and |
| 17 | `foo_baz.cc`. **Fix:** The `.h` and `.cc` files should come in |
| 18 | pairs, so either split `foo.h` into `foo_bar.h` and `foo_baz.h`, or |
| 19 | merge `foo_bar.cc` and `foo_baz.cc` into `foo.cc`. |
| 20 | |
| 21 | ## Exception for platform-specific code |
| 22 | |
| 23 | If the functions in a header file need different implementations for |
| 24 | different platforms, we allow the following arrangement: |
| 25 | |
| 26 | * Declarations in `foo.h`. |
| 27 | * A complete set of matching definitions in `foo_win.cc`, another |
| 28 | complete set of matching definitions in `foo_mac.cc`, and so on. |
| 29 | * As per the main rule, these files should all be in the same |
| 30 | directory and in the same build target. The build target should use |
| 31 | platform conditionals to ensure that exactly one of the `.cc` files |
| 32 | are included. |