0

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

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 Jul 27 '16 at 12:53
  • @diego9403 You could always just google * source code, such as iostream 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 Jul 27 '16 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.