About Include and CPP files

If you have two CPP files one called Main.cpp and one called Second.cpp, in Second.cpp you need to include <iostream> again that you already have included in Main.cpp if you use std::cout for example.

My question is when the compiler compiles everything it goes from up to down from the beginning in Main.cpp to Second.cpp wouldn't the compiler understand that <iostream> is already included? Or is it the an IDE error?

For example http://i.imgur.com/nMifc.png

I don't have a problem including <iostream> in Second.cpp I just want to know the logic behind it.
Last edited on
In all professional include files there's a part that looks like this:

1
2
3
4
5
6
#ifndef IOSTREAM_H
#define IOSTREAM_H

//file contents

#endif 


This part of the file says: if the variable IOSTREAM_H is not defined, then define it, and do the stuff in the //file contents. This is analysed at compile-time, NOT at run-time.

With this header in each header file, you won't have the problem of including stuff more than once. Actually if you deliberately include/define stuff more than once without such cautions, the compiler will definitely complain about it.
Last edited on
Each cpp is compiled separately.
After that they are linked together (to resolve the function definitions)
I'm aware of that, I'm just curious about why it needs to be included two times? From what I understand is that the compiler just put all text together. Same as if you put an int in Main.cpp and tries to use it in Second.cpp. You'll still need to use a pointer or the extern command to use the int in Second.cpp.

http://i.imgur.com/nMifc.png

from this picture from my understanding is that the compiler will compile it this way from line 1 to line 16.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void second();

int main() {

second();

return 0;
 }

void second() {

std::cout << "hello" << std::endl;

}


So why do you need to include <iostream> again in Second.cpp Is it because the IDE don't understand it? If use only a compiler and not an IDE like

gcc main.cpp second.cpp will it compile ?
Last edited on
My question is when the compiler compiles everything it goes from up to down from the beginning in Main.cpp to Second.cpp wouldn't the compiler understand that <iostream> is already included?


This is wrong. It does not go from the beginning in Main.cpp to Second.cpp . It goes from the start of Main.cpp to the end of Main.cpp ,and then stops. Then, it goes from the start of Second.cpp to the end of Second.cpp. They are compiled completely independently. Anything you put in Main.cpp has no bearing whatsoever on the compilation of Second.cpp

They are compiled as separate files. In Main.cpp, when the compiler gets to the function call second(), it does NOT go to a different file to compile that. It happily notes that there is a function call there, and carries on with the file it is doing.
Last edited on
So why do you need to include <iostream> again in Second.cpp Is it because the IDE don't understand it? If use only a compiler and not an IDE like


No, it's because Second.cpp isn't aware of the iostream library.
Okay, that changed my view to it and makes more sense hehe thanks for quick answers!

Last edited on
Topic archived. No new replies allowed.