C ++ HUGE compiled executables?

After programming for some time in C, I finally decided to start learning C ++. This bothers me, since the standard "hello world" in C is usually ~ 16K, including all the crap your compiler throws. (Using stdio)

However, when I create a C ++ executable that creates the hello world, the file is ~ 470KB! I went ahead and used cstdio instead of iostream, thinking that it would make a difference, and it happened.

My question is: When I turn on iostream, why does the size of my executable file explode?

Edit: I am using g ++ (using the Dev-CPP IDE, but I can figure out how to add CL parameters)

+5
source share
6 answers

In a word, symbols .

The C ++ Standard Library introduces many characters into your program, since most of the library exists mainly in header files.

Recompile your program in release mode and without debugging symbols, and you can easily expect that the program will be much smaller. (Less if you separate the characters.)

As a quick demonstration of this fact, note:

$ cat hello.c
#include <stdio.h>
int main() {
    printf("%s\n", "Hello, world!");
    return 0;
}
$ cat hello.cpp
#include <iostream>
int main() {
    std::cout << "Hello, world!\n";
    return 0;
}
$ gcc hello.c -o hello-c
$ g++ hello.cpp -o hello-cpp
$ gcc hello.c -ggdb -o hello-c-debug
$ g++ hello.cpp -ggdb -o hello-cpp-debug
$ gcc hello.c -s -o hello-c-stripped
$ g++ hello.cpp -s -o hello-cpp-stripped
$ gcc hello.c -s -O3 -o hello-c-stripped-opt
$ g++ hello.cpp -s -O3 -o hello-cpp-stripped-opt
$ ls -gG hello*
-rwxr-xr-x 1  6483 Nov 14 15:39 hello-c*
-rw-r--r-- 1    79 Nov 14 15:38 hello.c
-rwxr-xr-x 1  7859 Nov 14 15:40 hello-c-debug*
-rwxr-xr-x 1  7690 Nov 14 15:39 hello-cpp*
-rw-r--r-- 1    79 Nov 14 15:38 hello.cpp
-rwxr-xr-x 1 19730 Nov 14 15:40 hello-cpp-debug*
-rwxr-xr-x 1  5000 Nov 14 15:45 hello-cpp-stripped*
-rwxr-xr-x 1  4960 Nov 14 15:41 hello-cpp-stripped-opt*
-rwxr-xr-x 1  4216 Nov 14 15:45 hello-c-stripped*
-rwxr-xr-x 1  4224 Nov 14 15:41 hello-c-stripped-opt*

I can’t explain why building Windows with g ++ creates such large executables, but on any other platform, characters are a major factor in driving large file sizes. At the moment I do not have access to the Windows system, so I can not check.

+6
source

, iostreams. , .

, , , , / , . , , .

VC /MD, .

+7

, <iostream>, STL, <string>, , , <vector> ..

+2

( ), , - . MS V++, , ~ 8K ~ 110K. MinGW, 24-25K ( , ).

, , , , V++, . MinGW , , - , ; , , , .

+2

MinGW (g++) .
, " " iostreams ~ 100 V++ ( CRT) ~ 470 g++.

+1

This is one aspect of a fake C ++ interview that is actually true :)

You know, when we had our first C ++ compiler, on AT&T I compiled "Hello World" and could not believe the size of the executable. 2.1MB

What? Well, compilers have come a long way since then.

They have? Try it in the latest version of g ++ - you won’t get many changes from half a megabyte.

+1
source

All Articles