How does the Include directive work?

10

Does "include" of C ++ do exactly what? I know it "imports" a header / library. But if I have a Header.h with:

#include <string>
using namespace std;
string a() {
return "PTSO";
}

and in Main.cpp:

#include <string>
#include "Header.h"
using namespace std;
int main() {
return 0;
}

What exactly am I imported from Header? All its contents (includes ...), or just the functions?

    
asked by anonymous 24.12.2014 / 11:31

3 answers

5

You are importing text. What the compiler does is simplified the same as you copying and pasting the text that is inside the include into the text that is in the main font.

This is why the position in which it is included is important. Almost always when you do not do it at first you are in trouble.

It's importing all your content. Normally this content is just declarations of data structures (classes, structures, enumerations), constants, function / method header, macros and eventually some code when you want a function to be imported directly into the source instead of being called ( source inline ). The most common is that it does not have code. Usually it is used to declare the data structures but not to define the algorithms.

Ideally, do not abuse includes within includes . This is not always possible.

In this example the compiler will probably mount something like this to compile:

/*

todo conteúdo enorme do arquivo string.h aqui

*/

using namespace std;
string a() {
return "PTSO";
}

/*

todo conteúdo enorme do arquivo string.h aqui repetido já que ele já está lá em cima

*/

using namespace std;
int main() {
return 0;
}

There is a technique for avoiding the repeated inclusion of include files by accident. Which does not seem to be the case. Well, it's because you did not know you were wrong. But it is a case that you did deliberately. The crash occurs when it becomes complex and you can no longer control "eye" an include is already there in the code or not.

I will consider that this is just an example, because it makes no sense to use #include "Header.h" in the main code. It is including something that is not necessary.

Anyway, it would be normal to set the function a() within .cpp itself.

Whenever you can avoid using include , avoid. Of course in a real application this is almost impossible. Creating an include is required when you are going to use the algorithms defined in a .cpp in another source file. When you are sure to only use it inside yourself, you do not need to create a .hpp .

A better example

auxiliary.cpp

#include <string>

std::string a() {
    return "PTSO";
}

auxiliary.hpp

#include <string>

std::string a();

main.cpp

#include <iostream>
#include "auxiliar.hpp"

int main() {
    cout << a();
    return 0;
}

Note that in C ++ it is common to use .hpp , although a lot of programmer does not even try to reuse in C. What would you do with both extensions as well. I'd rather use .hpp to make it clear that code was written in C ++.

    
24.12.2014 / 12:06
4

The #include directive, when executed, causes a copy of the file whose name is given between < e > included in the source code. For example, suppose we define the following macros and save them in a file called macros.h:

#define quad(n) ( (n)∗(n) )
#define abs(n) ( (n)<0 ? −(n) : (n) )
#define max(x,y) ( (x)>(y) ? (x) : (y) ) 

So every time we need one of these macros, you do not have to type them again; just ask the preprocessor to include a copy of the macros.h file at the beginning of our program.

Two things to note about adding files:

  • Any file, with any extension, can be included in a source program through the #include directive.
  • The notation < e > is preferably used for standard C-inclusion files. To include user-defined files, use the "e" notation.
  • Source: Language C

        
    24.12.2014 / 12:03
    1

    "include" brings information about the library, in case your library has functions, it will bring everything in it.

        
    24.12.2014 / 12:01