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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Whereas:
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.
.h files, or header files, are used to list the publicly accessible instance variables and 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.
A header (
.h
,.hpp
, ...) file containsclass X { ... };
)inline int get_cpus() { ... }
)void help();
)extern int debug_enabled;
)A source file (
.c
,.cpp
,.cxx
) containsvoid help() { ... }
orvoid X::f() { ... }
)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.Others have already offered good explanations, but I thought I should clarify the differences between the various extensions:
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.
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.
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.