简体   繁体   中英

#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"?

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.

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

编号库(标题) 不会增加文件的大小,因为编译器不会将所有标题添加到您的代码中。它只是将您在代码中使用的内容添加到代码中,

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM