Frage

I've Googled this, but most of the results are about compiler A vs. compiler B.

This question arises from my experience with Web Design (know the languages commonly used on Web Design aren't compiled. CGI is an exception.) When you include file in a web page the entire file is downloaded to the user's computer. If you use JQuery, a JavaScript library, you must include JQuery in the <head> in order to use its functions. This got me thinking about C++ compilers.

In C++ it's practically impossible to program without #include <library>. Are the libraries compiled along with the project files? Is the

For example, in this simple Hello Word program #include <iostream>, is all of the code in the iostream header file compiled? Will taking ONLY the code that defines cout and adding it to this example make the size of the file smaller? Does compiler optimization take care of only compiling the necessary code from iostream?

#include <iostream>

int main()
{
   std::cout<<"Hello World.";
   return 0;
}
War es hilfreich?

Lösung

For example, in this simple Hello Word program #include , is all of the code in the iostream header file compiled? Will taking the ONLY the code that defines cout and adding it to this example make the size of the file smaller? Does compiler optimization take care of only compiling the necessary code from iostream?

Yes and no. C++ includes are a remarkably simple mechanism. The included header really is just concatenated with the including file, so yes, the simple answer is that you get everything you include.

But once everything has been included in this manner, the compiler does try to eliminate all the bits that aren't used. For many (not all) headers, this is easy to observe: if you compile a simple Hello World program, look at the file size, and then include a few more headers and recompile it, the executable size will typically be the same (or almost the same). The compiler determines that much of the cruft you included is never used, and removes it again.

There are exceptions, however. Some headers, just by being included, cause code to be run when the application is launched, for example, meaning that even if you don't refer to anything included from the header, the compiler can't remove it.

Andere Tipps

All compilers have their own implementation of the STL - some are open source (gcc), and some only include headers. In most cases, these implementations have been compiled down to a library to speed up the compilation process of your program. The headers only serve as a hint to the compiler about what these compiled libraries contain - sort of like a guide to reading the contents of the library. They are not compiled themselves.

If this library is static, then the linker stage takes data from the library and copies it into your executable. If it's a dynamic library (a .dll, .so, or .dylib), then no data is copied - your executable simply calls upon data in those files at runtime. This might be via a function address or the name of a symbol, depending on how your dynamic library is linked (in most cases it is the former).

Compilers have an optimization stage that gets rid of unused data. If you included iostream but did not use any of its contents, for example, then none of that data will be linked to your program. Depending on the compiler, if you only use one overload of cout::operator<<, then only that overload and the functions it calls will be linked. Since the STL is a template library, it is compiled along with your executable. This is because there is typically an infinite number of possible results - IE std::vector, which takes any type except references, can never be compiled down to a library because it acts differently depending on the type you give it.

MSVC has a tool that you can use to look at the compiled assembly of your executable in correspondence to its code and the code of included libraries. If you're using MSVC++ 2013, then pressing alt-8 during the debugging of your process will launch this tool, called a disassembler. It's a good way to visualize your program, if you're interested in seeing all of this information in action.

Headers like iostream define various types, functions and sometimes a globals.

The library files are precompiled source code that implement the functions/classes of the header files. cout is defined in the header file but resides within the library.

Well, normally, the compiler makes sure to add only the bits of the code that is needed for the program to run. However there are cases when #include does make the code bigger. One such case is if the library uses inline code which does increase the size.

Libraries are pre-compiled. your programs are linked with the libraries. (like runtime c++ lib)

If you are using static libraries, the linker will copy the used methods into your executable.

If you are using shared libraries (so, DLL, etc.) it save reference in the executable to the linked methods, instead of coping into the executable file.

Include files are not pre-compiled. They consist some API to a class or lib. usually only prototypes to the methods, but sometimes all the source (like in template classes) Using the #include macro means like copy the whole header file into your source file.

More thing there are some steps during compilation:

  1. pre-process - add all the include files in place. macros resolve etc. output is a source file much bigger, ready for compilation.
  2. compile - compile each source file to machine language, output binary files (object file)
  3. link - get all compiled files, and all used libraries and generate the executable.

The compiler compile all the source code, but in the link the linker take only the used code and add it to the executable, no matter if the code is in your source code in the header or in the libraries. if it not been used it won't be added to the executable.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top