Because I've made .cpp
files and then transferred 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?

- 4,271
- 8
- 34
- 56
-
4To understand people's answers below, you might need to ask another question: i.e. "What's the difference between a 'declaration' and a 'definition' in C++?" – ChrisW May 17 '09 at 21:00
-
1I know the difference between a declaration and a definition. – May 17 '09 at 21:23
-
3You can include anything you want, but linking stage will break if you include global variables into more than one compilation units. That's why there is a convention to not link .cpp files. – John Smith May 17 '09 at 22:16
8 Answers
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 #include
d -- we include the declarations.

- 1,498
- 1
- 12
- 17
-
The C++ build system does differentiate - try running g++ on a .h file and a .cpp file and see what happens, for example. – May 17 '09 at 21:09
-
1I know that .cpp files are used (most of the time) to define functions/classes/etc that have been declared in .h files, but you can define variables in .h files too. – May 17 '09 at 21:25
-
5@Neil Butterworth: Yes the G++ compiler understands the convention, but you can make it compile by forcing the language. – Martin York May 18 '09 at 15:03
-
1This is like saying "Windows knows no difference between a `.png` and a `.doc`". At some level, they’re both just files and Windows doesn’t know the difference. But Windows is configured to treat them differently. Many C++ build systems _are configured_ to know the difference. For example, by default Visual Studio puts `.cpp` files in `
` tags in its `vcxproj` files, but header files included in the same project are in ` ` tags. XCode won’t list `.h` files under “Compile Sources” in its “Build Phases” tab, but it will list `.c`, `.cpp`, `.m` and `.mm` files there. – bobobobo Sep 29 '21 at 00:02
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 copied/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.

- 4,271
- 8
- 34
- 56

- 67,274
- 36
- 133
- 188
-
1
-
12@Keand64: Because the compiler looks at only *one* .cpp file at a time when compiling things. It reads a .cpp file, reads all the #included .h files, compiles the whole thing, writes a .o (or .obj) file, and then proceeds to the next .cpp file. – Greg Hewgill May 17 '09 at 21:50
-
-
@fathergorry No, there is no rule for naming source files (headers or not), only conventions. – Klaim Mar 09 '20 at 17:11
.h
files, or header files, are used to list the publicly accessible instance variables and methods in the class declaration. .cpp
files, or implementation files, are used to actually implement those methods and use those instance variables.
The reason they are separate is because .h
files aren't compiled into binary code while .cpp
files are. Take a library, for example. Say you are the author and you don't want it to be open source. So you distribute the compiled binary library and the header files to your customers. That allows them to easily see all the information about your library's classes they can use without being able to see how you implemented those methods. They are more for the people using your code rather than the compiler. As was said before: it's the convention.

- 4,271
- 8
- 34
- 56

- 19,083
- 4
- 59
- 71
-
5i know he didn’t ask why but this is the most helpful answer for me. understanding the why makes it easier to connect the logic to the details – vampiire Apr 17 '20 at 22:22
A header (.h
, .hpp
, ...) file contains
- Class definitions (
class X { ... };
) - Inline function definitions (
inline int get_cpus() { ... }
) - Function declarations (
void help();
) - Object declarations (
extern int debug_enabled;
)
A source file (.c
, .cpp
, .cxx
) contains
- Function definitions (
void help() { ... }
orvoid X::f() { ... }
) - Object definitions (
int debug_enabled = 1;
)
However, the convention that headers are named with a .h
suffix and source files are named with a .cpp
suffix is not really required. One can always tell a good compiler how to treat some file, irrespective of its file-name suffix ( -x <file-type>
for gcc. Like -x c++
).
Source files will contain definitions that must be present only once in the whole program. So if you include a source file somewhere and then link the result of compilation of that file and then the one of the source file itself together, then of course you will get linker errors, because you have those definitions now appear twice: Once in the included source file, and then in the file that included it. That's why you had problems with including the .cpp
file.

- 496,577
- 130
- 894
- 1,212
-
As you have correctly mentioned, it's a header file that contains a class definition, not a source file. – Meisam Rasouli Jun 24 '22 at 14:32
I know the difference between a declaration and a definition.
Whereas:
- A CPP file includes the definitions from any header which it includes (because CPP and header file together become a single 'translation unit')
- A header file might be included by more than one CPP file
- The linker typically won't like anything defined in more than one CPP file
Therefore any definitions in a header file should be inline or static. Header files also contain declarations which are used by more than one CPP file.
Definitions that are neither static nor inline are placed in CPP files. Also, any declarations that are only needed within one CPP file are often placed within that CPP file itself, nstead of in any (sharable) header file.

- 54,973
- 13
- 116
- 224
A good rule of thumb is ".h files should have declarations [potentially] used by multiple source files, but no code that gets run."

- 11,957
- 4
- 29
- 37
By convention, .h files are included by other files, and never compiled directly by themselves. .cpp files are - again, by convention - the roots of the compilation process; they include .h files directly or indirectly, but generally not .cpp files.

- 224,562
- 31
- 268
- 324
Others have already offered good explanations, but I thought I should clarify the differences between the various extensions:
Source Files for C: .c Header Files for C: .h Source Files for C++: .cpp Header Files for C++: .hpp
Of course, as it has already been pointed out, these are just conventions. The compiler doesn't actually pay any attention to them - it's purely for the benefit of the coder.

- 144,213
- 56
- 264
- 302
-
7
-
@Zifre: Yes, hence my saying that these are *recommended naming conventions*, even though many may not follow them. – Noldorin May 17 '09 at 22:05