How does the "#include" directive work?

Asked

Viewed 6,237 times

15

C++ "include" does what exactly? I know it "matters" a header/library. But if I have a Header. h com:

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

and on Main.cpp:

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

What exactly am I imported from Header? All your content?(includes...) or just the features?

4 answers

10


You are importing a text. What the compiler does is simply the same as you copy and paste the text that is inside the include into the text that is in the main source.

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

He’s importing all your content. Usually 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 rather than called (source inline). The most common is that it has no code. Usually it is used to declare the data structures but not to define the algorithms.

The ideal is not to abuse includes inside includes. This is not always possible.

In this example the compiler will probably assemble 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 to avoid repeated inclusion of files include by accident. Which doesn’t seem to be the case. Well, it’s because you did it without knowing it’s wrong. But it’s a case you did deliberately. The accident occurs when it becomes complex and you can no longer control the "eye" of a 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 required.

Anyway the normal would be to define the function a() within the .cpp even.

Whenever you can avoid using include, avoid. Of course in a real application this is almost impossible. Create a include is necessary when you will use the algorithms defined in a .cpp in another source file. When it is certain that it will only use there inside itself, do not need to create a .hpp.

A better example

auxiliary.cpp

#include <string>

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

auxiliary.

#include <string>

std::string a();

main.cpp

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

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

I put in the Github for future reference.

Note that in C++ it is common to use .hpp, although many programmer does not even to try to reuse in C. What would also give with both extensions. I prefer to use .hpp to make it clear that that code was written in C++.

  • I get it. I put Header. h just to have a header, rs.

  • 1

    Beyond the .hpp, some use the extension .hxx or no extension for C++headers. I prefer to use .hpp for the same reason.

7

The directive #include, when executed, causes a copy of the file whose name is given between < and > to be included in the source code. For example, suppose we define the following macros and save them to 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 don’t have to type them again; just ask the Preprocessor to include a copy of the macros file. h at the beginning of our program.

Two remarks should be made regarding the inclusion of archives:

  1. Any file, with any extension, can be included in a source programme through the Directive #include.
  2. Notation < and > is preferably used for standard C language inclusion files. To include user-defined files, use notation " and ".

Source: Language C

  • This copied excerpt is without context. I read it and got lost in the explanation. Aside it’s about C and not C++ (although it’s very similar).

  • I took from the C booklet, by the same similarity, the way it works is equal in both.

2

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

  • 3

    The answer is not enough to be wrong but this is a huge simplification of the process. Header files are not directly linked to libraries. Eventually they are used for this.

1

You’re importing a text. What the compiler does is simply the same as you copy and paste the text inside include into the text that is in the main source.

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

He’s importing all your content. Usually 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 rather than called (source inline). The most common is that it has no code. Usually it is used to declare the data structures but not to define the algorithms.

The ideal is not to abuse includes within includes. Not always this is possible.

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

Browser other questions tagged

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