Harsh Pathak
Harsh Pathak

Reputation: 85

#include <iostream> in 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"?

Upvotes: 7

Views: 3015

Answers (5)

Pete Becker
Pete Becker

Reputation: 76523

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

It's a little more subtle than that. #include "file" does what you describe. #include <file> pulls in a header which is not required to be a file. The idea is that the compiler can have its own internal version of that header, already in binary form, so it doesn't have to read and parse a file. If there is no header with that name, then it treats the include directive as if it had been #include "file" and looks for a file to read.

In practice, I don't know of a compiler that takes advantage of this leeway; all headers are, in fact, files that get compiled over and over again. But, as others have said, that in itself does not mean that the executable file becomes larger. Ideally, if you don't use it, it doesn't get into the executable, although some systems are better about this than others.

Upvotes: 0

e.jahandar
e.jahandar

Reputation: 1763

No, it doesn't increase program size. The file iostream and many other header files don't have compilable statements. They contain some definitions required for you program. If you look at a preprocessed C/C++ file, you will see thousands of definitions are added at the beginning of the file.

You can try it with cpp tool, run it with commands just like normal g++/gcc and see the outputs.

cpp -I /usr/include/ -I. file.cpp -o file.preprocessed

It contains just the headers and definitions, they don't increase you final program size.

Edit: As martin said, they have compilable inline functions which will compile each time, but they doesn't increase your program size unless you use them.

Upvotes: 0

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.

Upvotes: 12

Shubham
Shubham

Reputation: 2855

Regarding the size of intermediate file size, yes it will increase. You can check this by writing a simple hello world program and then compile it as follows (in Linux)

g++ name.cpp -o name --save-temps

This will store intermediate files, specifically:

"name.ii" (Preprocessed code after including <iostream>
"name.s"  (Assembly version of the code)
"name.o"  (Object code)

Check the difference in this size of file using:

ls -l name.cpp name.ii

Upvotes: 3

Leo
Leo

Reputation: 912

No.libraries(headers) don't increase the size of file because compiler don't add all of the header into your code.It just add things you use in your code to your code,

Upvotes: 0

Related Questions