What is the difference between a .cpp file and a .h file?


Question

Because I've made .cpp files then transfered them into .h files, the only difference I can find is that you can't #include .cpp files. Is there any difference that I am missing?

1
53
5/18/2009 4:56:03 PM

Accepted Answer

The C++ build system (compiler) knows no difference, so it's all one of conventions.

The convention is that .h files are declarations, and .cpp files are definitions.

That's why .h files are #included -- we include the declarations.

52
5/17/2009 8:57:28 PM

The .cpp file is the compilation unit : it's the real source code file that will be compiled (in C++).

The .h (header) files are files that will be virtually copy/pasted in the .cpp files where the #include precompiler instruction appears. Once the headers code is inserted in the .cpp code, the compilation of the .cpp can start.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon