1

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;
}
Andrue
  • 688
  • 3
  • 11
  • 27
  • 1
    The mistake is that you are thinking about headers as libraries. Check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329) to find an introductory book on C++. – IInspectable Jan 05 '14 at 17:23
  • @IInspectable think I see when I worded it wrong. `iostream` is part of the Standard Template Library, right? I haven't read up on that the Standard Template Library yet; it's at the back of the book. – Andrue Jan 05 '14 at 17:28
  • @Crysis it is part of the C++ standard library. The STL is a kind of vague term, but usually, it is more correct to talk about the standard library. The STL was originally a separate library consisting basically of containers, iterators and algorithms, but was incorporated into the C++ standard library. The headers `, `` and the various containers, such as ``, `` and `` came from the STL in this manner. But `` did not. It was there before the STL (and its age shows. It is an awful API compared to the STL-based stuff). – jalf Jan 05 '14 at 19:36
  • So loosely speaking, the STL is a part of the C++ standard library, but the C++ standard lib contains many other parts as well. – jalf Jan 05 '14 at 19:37
  • @Crysis : If your book doesn't mention the STL until the end then you have a terrible book. – ildjarn Jan 06 '14 at 01:53
  • @ildjarn It mentions the STL along the way, but in Chapter 16 it goes into greater depth. I looked at C++ for Dummies and it did the same thing. Chapter 16 for "The string Class and the STL". Classes are started in chapter 10, after covering the stuff like loops, logic, and functions. It covers a lot with arrays, but I already know how to use vectors, and they're much easier to use than arrays for the most part. I said Ch 10 for Objets and Classes; "And Intro to Design Patters in C++ with Qt (2nd Edition)" mentions classes in Chapter 2 and doesn't mention overloading functions until after QT. – Andrue Jan 06 '14 at 03:42
  • I'm using the most recent edition of C++ Primer Plus. It's the number 1 result in the C++ Book Reference mentioned by IIspectible. – Andrue Jan 06 '14 at 03:56
  • @Crysis : The book list recommends [C++ Primer](http://www.amazon.com/Primer-5th-Stanley-B-Lippman-ebook/dp/B0091I7FEQ/), which is a very different book than [C++ Primer Plus](http://www.amazon.com/Primer-Plus-Edition-Developers-Library/dp/0321776402/), and unfortunately for you, the latter is notoriously unsatisfactory. – ildjarn Jan 06 '14 at 04:54
  • @ildjarn just got C++ Primer 5th Edition. See, what happened was, I torrented the book, and I guess the torrent was wrong. It didn't say Primer Plus on the torrent, and I haven't thought anything about it since. What exactly is wrong with Primer Plus? It seems to explain the code well; admittedly, the pointer and address section didn't go well. – Andrue Jan 06 '14 at 05:04
  • It's not inaccurate as far as I know, it just isn't as good as _many_ other books, mostly because it puts too much emphasis on the wrong things and vice versa. – ildjarn Jan 06 '14 at 05:34
  • @ildjarn Should I start rereading everything I've learned so far? Most of it has been a review, anyway. – Andrue Jan 06 '14 at 05:37
  • It's not my place to say, but I'd try the first few chapters of Accelerated C++ for contrast. – ildjarn Jan 06 '14 at 05:48
  • Just saying, statically Linked stdio.h is 0.5mb but statically Linked iostream is 1.5mb. Why ? – 0xB00B Feb 25 '21 at 05:52

5 Answers5

3

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • ``, for example. It declares some global objects (`std::cout` for example), whose constructors are run before `main` is called – jalf Jan 05 '14 at 19:32
2

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.

NmdMystery
  • 2,778
  • 3
  • 32
  • 60
1

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.

SHR
  • 7,940
  • 9
  • 38
  • 57
  • So that's what a DLL does. I've always assumed it did something similar. – Andrue Jan 05 '14 at 17:30
  • "Libraries are pre-compiled..." Not necessarily, they may consist of headers only. – juanchopanza Jan 05 '14 at 17:35
  • 1
    1. I didn't mentioned TLB files, which are slight different and contain interfaces, since it not widely used. 2. libraries with only headers are include files not libraries. – SHR Jan 05 '14 at 17:42
1

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.

egur
  • 7,830
  • 2
  • 27
  • 47
1

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.

Adrian Nițu
  • 131
  • 1
  • 2
  • 11
  • A compiler cannot do what you describe. The linker, on the other hand, can. – IInspectable Jan 05 '14 at 17:32
  • @IInspectable, what is the difference between the compiler and the linker? I assume the compiler compiles (duh), but why is a linker required? Is the linker pre-compile? – Andrue Jan 06 '14 at 03:43
  • @Crysis The compiler turns human-readable source code into executable object code. The linker produces the final executable image by linking the object code together, resolving referenced symbols (function calls and objects). As a by-product the linker knows when a symbol is unreferenced and can be evicted from the final executable image. – IInspectable Jan 06 '14 at 08:24
  • @IInspectable alright. Thanks. So, is the most of the compiler optimization actually the linkers? – Andrue Jan 07 '14 at 22:52