I understand that header files have their uses, especially in the case of interfaces, but is there any reason you shouldn't include a cpp file directly? You can still use include guards and/or pragma once inside a cpp so is there really any reason besides maybe going against standard practice?
Asked
Active
Viewed 83 times
0
-
1If you include all files of a project in every [translation unit](https://en.wikipedia.org/wiki/Translation_unit), then you are defeating the purpose of having separate translation units. Of course, you may dump all of your code into a single file or translation unit, but this can get messy very quickly, especially with larger programs. It also increases compile time when you make changes, because you must then recompile all the code instead of only one translation unit. – Andreas Wenzel Jun 08 '20 at 17:26
-
2If you include the same `.cpp` files into diferent translation units, then you will also be violating the [one definition rule](https://en.cppreference.com/w/cpp/language/definition). – Andreas Wenzel Jun 08 '20 at 17:30
1 Answers
1
We have header files for a reason. It (mostly) separates the interface of some code from its implementation. Some of the benefits include the compiler having to go through less code and not having to recompile every file that uses a function when you change its definition. Including source files can also violate ODR as pointed out in the comments.

eesiraed
- 4,626
- 4
- 16
- 34