9

Where should I include header files, in the .cpp or in the .h file?

I am working on a project now and trying to come up with a clean way to follow once and for all. The state now is, files are included in both .cpp and .h files. For example, sometimes they have #include in someFile.h and sometimes in someFile.cpp.

I wonder, does it matter where you do the include? It works both ways, but are there any pitfalls?

Thanks

user2381422
  • 5,645
  • 14
  • 42
  • 56
  • Generally, use `struct`/`class` type declarations in .h files, where only the pointer type is needed. Do the include in the .cpp. This helps avert circular include problems. – Hot Licks May 29 '13 at 19:04
  • @HotLicks doesn't a good include guard against circular includes? – ott-- May 29 '13 at 19:05

2 Answers2

16

In general, you should only include headers in .h files that are needed by those headers. In other words, if types are used in a header and declared elsewhere, those headers should be included. Otherwise, always include headers only in .cpp or .c files. This keeps compilation time to a minimum and better shows what files are needed.

An exception is to include very commonly used headers in one standardized .h file, often called Stdafx.h, and then enable pre-compiled headers.

shipr
  • 2,809
  • 1
  • 24
  • 32
6

Include whatever header that is required by the code in the current file. No matter whether it is a .h or .cpp file

GuLearn
  • 1,814
  • 2
  • 18
  • 32
  • any reason for the downvote? – GuLearn May 29 '13 at 19:04
  • 1
    That actually matters *a lot* in term of compilation speed. – zakinster May 29 '13 at 19:05
  • 1
    Do you have a way to avoiding include a header file in .h file when that header is *required* by the code in the header?! – GuLearn May 29 '13 at 19:06
  • 1
    @YZ.leaner Forward declaration as instance, but if you really *need* the header then there's really no question, you don't have a choice. I suppose the question is raised when it's the `.cpp` that actually need the header, not the corresponding `.h`. – zakinster May 29 '13 at 19:09
  • I totally agree with what you said. But when you could do forward declaration, you are not *required* to include the header file in the .h file. Also, with the `#ifndef... #define` guard, each header will be actually included exactly once. So there is no difference in the compiled binary – GuLearn May 29 '13 at 19:12
  • 2
    I realize now that I may have misread your answer. I actually agree with what you're saying, but I'm not sure whether it actually answer the question. The binary would indeed be the same, but the compilation time won't. If `foo.cpp` needs `bar.h` and you put the include in `foo.h` instead of `foo.cpp`, then you will include `bar.h` in the compilation of every `.o` that needs `foo.h` even if it probably doesn't need `bar.h`, hence reducing the compilation performance. – zakinster May 29 '13 at 19:20
  • Does this mean we should duplicate include statements between `.h` and `.cpp` files? – Stevoisiak Aug 23 '18 at 18:56