Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include <iostream> in C++?

Tags:

c++

I read that #include <file> will copy paste “file” into our source file by C++ preprocessor before being compiled.

Does this mean that the “file” (iostream) will also be compiled again and again as long as we compile source file?

Also after C++ does its job, will the size of intermediate file also have bytes of “file” + "size of source file"?

like image 785
Harsh Pathak Avatar asked Dec 13 '16 08:12

Harsh Pathak


1 Answers

I read that #include <file> will copy paste “file” into our source file by C++ preprocessor before being compiled.

Yes. The data that the compiler proper sees will consist of the data in file and the data in your source file. (Actually, real compilers these days tend to merge the C preprocessor and the compiler front end and the compiler back end into a single program - but that is pretty much a detail.)

Does this mean that the “file” (iostream) will also be compiled again and again as long as we compile source file?

Yes. Some compilers have a feature called "pre-compiled headers" which allow you to compile a bunch of headers once, and then use the output of that multiple times. How you do this varies from compiler to compiler; if you need portability, don't worry about it (it doesn't make that big a difference to compile times).

Also after C++ does its job, will the size of intermediate file also have bytes of “file” + "size of source file"?

No. The size of the output file is only very weakly related to the size of the source file. For example #include <iostream> defines many, many inline functions. Any particular program will only use a very few of those - so they will be omitted from the compiler output.

Comments (which use space in the source file) don't appear in the output.

On the other hand, if you write a complex template, and then instantiate it for several different types, then the output will contain a different copy of the template for each type, and may be quite a bit larger than the input.

like image 78
Martin Bonner supports Monica Avatar answered Sep 30 '22 00:09

Martin Bonner supports Monica