I am wondering how compiler (or preprocessor) include headers. I have three files. First is header.h, second first.cpp <- and there is #include <iostream>
, third <- it contains #include <iostream>
too.
header.h:
#ifndef HEADER_H_
#define HEADER_H_
struct Student
{
std::string imie;
int ocena;
};
void foo();
#endif
first.cpp:
#include <iostream>
#include "header.h"
int main()
{
Student Jan;
std::cout << "It's OK" << std::endl;
foo();
return 0;
}
Second.cpp:
#include <iostream>
#include "header.h"
void foo()
{
std::cout << "I like Linux" << std::endl;
}
And my question is do the compiler copy code from library twice? Should I cut all iostream
from .cpp's to one .h?
1 Answer 1
If you open the source code of the iostream
header, you will see it includes header guard as well, meaning the header will be included only once.
For example the GCC guards the header with the _GLIBCXX_IOSTREAM
constant.
-
Thank you for help, but where can I find all libraries (in Linux (this is more important for me) and Windwos)?diego9403– diego94032016年07月27日 12:53:23 +00:00Commented Jul 27, 2016 at 12:53
-
@diego9403 You could always just google
* source code
, such asiostream source code
. With closed-source libraries you are obviously going to have less luck and need to trust the creator of the library it was written well.Andy– Andy2016年07月27日 14:15:59 +00:00Commented Jul 27, 2016 at 14:15