How to create a file.h?

10

What is a arquivo.h and what improvement does it bring to the C ++ program?

    
asked by anonymous 27.07.2015 / 03:36

2 answers

15

Files with .h or .hpp extension as they like some to differentiate from C header files, are often used to put code needed to compile other parts of an application.

When you create an application, or even a library, some information is required to compile. Examples are classes, structures, static constants, and function declarations (so-called prototypes). In the case of classes and structures we are talking about several elements, including signing the methods.

Basically what is not necessary for compilation are the algorithms, ie the code inside the functions or methods. These can be compiled into binary code and are no longer required for the build process. At least in most cases.

There are still cases where you want to inline of the code or template ).

This file is a way to organize the code and make it easier to include this code in other files.

Contrary to what some may think, this file has normal code that could well be in a .cpp file. Of course, if all of these statements are in the .cpp file, you lose some flexibility. It's complicated to use your content elsewhere.

A file that includes a .hpp after preprocessed passes has the contents of .hpp as always written in it. That is, the file is as if it were copied to the file to be compiled.

Then in the file .cpp you have the definition of the algorithms and in .hpp you have the declaration. Note that this is a universally adopted convention but nothing prevents you from doing it differently. There is no reason to invent another way. With it you can have gains in compile time, and better organize what data structure and algorithm is.

If you have a .cpp file like this:

void teste();

void main() {
    teste();
}

void teste() {
    printf("teste");

You can either use the teste() function in this file or else you need to include the code together, which can be complicated by main() , as well as spending time compiling something that should already be compiled. Of course it is possible to save ( #ifdef ) the excerpt to not include what you do not need, but it can end up cumbersome the normal compilation of the file and put something in the file that you do not need for it.

If so separated in teste.hpp :

void teste();

And main.cpp looks like this:

void main() {
    teste();
}

void teste() {
    printf("teste");

You can use the teste() function anywhere else. Let's imagine that you create a new file .cpp like this:

#include "teste.hpp"
void funcao() {
    teste();
}

This works because the function declaration will be included in the file even if the effective function code is not. After preprocessing this code will be:

void teste();
void funcao() {
    teste();
}
    
27.07.2015 / 04:12
3

Friend, the .h file is a header, a configuration file, there you can define constants, include libraries, things that will be used in your main. You create a .h and include it in your main program.

Separating these features, you have greater maintainability in your source code, for example if you want to use another library, just insert it into .h and your program will automatically use it, and you can also use the same .h in other programs.

I hope I have helped.

    
27.07.2015 / 04:10