What are files with extension . cpp and . h?

Asked

Viewed 13,368 times

5

What are these files with extensions .cpp and .h? How they interact with each other ?

4 answers

13


These are C language source files++ (.cpp) and C (h), although everything that can be used in C can be used in C++ as well and some programmers use this extension even in C++. Ideally, for many, when in C++ the most appropriate extension is .hpp.

The .h or .hpp conceptually are header files and have code that the compiler needs to compile other parts. In general it contains only structured data (classes for example) with the signatures of methods and functions, and implementations that must be linearized or templates. Nothing prevents using other extensions for this, but this is the default.

The archive .cpp have the implementations, what after compiled does not matter anymore to those who use what is there in the source. It’s also just a convention, could use another extension.

Usually within the .cpp you "call" the .hpp with a compilation directive #include for the compiler to have access to what has been defined in this header. There may be include inside other headers as well.

Not that it’s strictly necessary to do that, should only create a header when the code should be used generically in other parts. It is possible to program without headers, but in complex applications it is almost impossible without abandoning code reuse.

There is a small difference in the use of include.

7

Archives .cpp are the files that contain the C++ implementations fonts. Files .h are called headers, used to extract the declaration of functions, classes and other statements from the implementation, allowing reuse.

In this answer i talk about the C file compilation process. In general terms, it is very similar to C++ for containing pre-processing of the source file, object file generation and subsequent linkage.

1

.cpp are C++ and . h source codes are C headers (although they can be used in C++) C++ has its own headers extension. hpp

0

The files . cpp is the source of your c++ code and . h are header files.

When you do a include you are adding a library to your code:

#include "header.h"
  • 4

    They are not libraries. Both are source code. Library is something else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.