2

Last week I found a simple game on the web called killallcops.exe. I studied the binaries and wondered why the programm is that big. (About 460k)

So I rewrote the game using iostream

#include <iostream>
int main(){
    std::cout << "All cops are killed.\nCongratulations";
    std::cin.ignore();
    return 0;
}

compiled it with g++ and got 460k too.

I really wondered why this thing was so big and rewrote it using stdio

#include <stdio.h>
int main(){
    printf("All cops are killed.\nCongratulations");
    while(getchar() != '\n'){}
    return 0;
}

compiled it with gcc and got 13k.

I know that iostream has much more abilities than stdio (like typesafety) and thus is itself much bigger, but why does this result in bigger binaries and are there solutions to reduce the size when using iostream, for example only using specific parts of it?

edit: as stated below using gcc 3.4.4 under cygwin

Baarn
  • 673
  • 11
  • 23
  • Can you show us the assembly it compiles to? – Richard J. Ross III Dec 24 '11 at 15:30
  • 2
    `` has a lot of internal dependencies, even for using a simple `cout` call. It brings in a lot of other code/headers it needs to work (which includes template code and such). You can trust the compiler/linker to know what they're doing, but if you want to reduce the binary size, try using options like `gcc -s`, which strips symbols, or flags like `-Os` which attempts to optimize the binary for size. However, in nontrivial apps the size of bringing in `` is not very significant. – wkl Dec 24 '11 at 15:31
  • @birryree: That could be an answer... :) – cHao Dec 24 '11 at 15:34
  • Are you using optimizations and stripping out symbols? – Kerrek SB Dec 24 '11 at 15:40
  • 1
    @walter you might be interested in [this quesion](http://stackoverflow.com/questions/1042773/gcc-c-hello-world-program-exe-is-500kb-big-when-compiled-on-windows-how/2250182#2250182) – greatwolf Dec 25 '11 at 09:52

3 Answers3

3

I'll flesh out my comment as an answer:

<iostream> has a lot of internal dependencies, even for using a simple cout call. It brings in a lot of other code/headers it needs to work (which includes template code and such). Up front let's say the size of bringing in <iostream> is probably not very significant in a nontrivial application.

You can trust the compiler/linker to know what they're doing, but if you want to reduce the binary size, try using options like gcc -s, which strips symbols, or flags like -Os which attempts to optimize the binary for size.

I suspect a lot of your binary size problems actually comes from something else: a statically-linked libstdc++.

If you're using MinGW on Windows, do note that until very recently, their implementation of the GCC toolchain did not have a dynamically-linked libstdc++; instead all C++ builds statically link in libstdc++ which will greatly increase the size of your binaries.

Here's a comparison, the size of a binary produced by g++ for your code, on Linux, using GCC 4.6.1. No optimization or symbol stripping was performed on these builds.

λ > ls -lh a.out          
-rwxr-xr-x 1 billylee billylee 6.1K Dec 24 10:37 a.out

And here's one produced by GCC

λ > ls -lh trial
-rwxr-xr-x 1 billylee billylee 4.9K Dec 24 10:41 trial

The g++ version is a little larger than the gcc version, but not 100x larger.

edit: If you're using MinGW - there's a recent thread here: How to reduce the size of executable produced by MinGW g++ compiler?

MinGW gcc 4.5.0+ should use dynamic linking by default, check your version if you are using MinGW.

Community
  • 1
  • 1
wkl
  • 77,184
  • 16
  • 165
  • 176
  • yes, i forgot to mention that i am using cygwin gcc. just checked, version 3.4.4 i will update and check back later. thanks for the explanation. – Baarn Dec 24 '11 at 15:53
  • updating cygwin gcc was a bigger problem, but i tried it out on the universities linux box (mine electrocuted itself a few days ago) and get similar results now. thanks – Baarn Dec 29 '11 at 20:51
2

I don't reproduce on Linux (sizes are 9K and 8K). My guess is that for the C++ you are statically linking the standard library while for C you are dynamically linking it.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
2

The answer is: it isn't that big. Compiled on c++ 4.6 on Linux with no compile options it is 9040 bytes. After stripping it's 6312 bytes.

What compile options did you use and what version of g++ are you using?

It's possible that the size is due to your platform, perhaps if you are using MinGW and it is statically linking the runtime.

On a side note, #import is a deprecated g++ only extension to the language, you should be using #include

lefticus
  • 3,346
  • 2
  • 24
  • 28
  • it indeed was include, was using too much java the last few days, so that error slipped in when writing the code from my memory. – Baarn Dec 24 '11 at 15:51
  • @left if I recall, when you compile on linux by default the rtl isn't linked in statically where as on windows it is. So of course it's going to be smaller, it's not an equal comparision. – greatwolf Dec 25 '11 at 09:55