English 中文(简体)
g++ produces big binaries despite small project
原标题:

Probably this is a common question. In fact I think I asked it years ago... but I can t remember the answer.

The problem is: I have a project that is composed of 6 source files. All of them no more than 200 lines of code. It uses many STL containers, stdlib.h and iostream. Now the executable is around 800kb in size.... I guess I shouldn t statically link libraries. How to do this with GCC? And in Eclipse CDT?

EDIT: As I responses away from what I want I think it s the case for a clarification. What I want to know is why such a small program is so big in size and what is the relationship with static, shared libraries and their difference. If it s a too long story to tell feel free to give pointers to docs. Thank you

最佳回答

If you give g++ dynamic library names, and don t pass the -static flag, it should link dynamically.

To reduce size, you could of course strip the binary, and pass the -Os (optimize for size) optimization flag to g++.

问题回答

One thing to remember is that using the STL results in having that extra code in your executable even if you are dynamically linking with the C++ library. This is by virtue of the fact that the STL is a bunch of templates that aren t actually compiled until you write and compile your code. Since the library can t anticipate what you might store in a container, there s no way for the library to already contain the code for that particular usage of the container. Same goes with algorithms and everything else in the STL.

I m not saying this is definitely the reason your executable is so much larger than you expect. But it may be a factor.

Use -O3 and -s flags to produce the most optimized binary. Also see this link for some more information.

If you are building for Windows, consider using the Microsoft compiler. It always produces the smallest binary on that platform.

Eclipse should be linking dynamically by default, unless you ve set the static flag on the linker in your makefile.

In response to your EDIT :

-when you link statically, the executable contains a full copy of each library you ve linked to.
-when you link dynamically, the executable only contains references and hooks to the linked libraries, which is a much much smaller amount of code.

The executable has to contain more than just your code.

At the very least, it contains some startup code, setting up the environment and if necessary, loading any external libraries, before the program launches.

If you ve statically linked the runtime library, you also get that included in your executable. Otherwise you only get a small stub, just big enough to redirect system calls to the external runtime.

It may, depending on compiler settings also include a lot of debugging info and other non-essential data. If optimizations are enabled, that may have increased code size as well.

The real question is why does this matter? 800KB still fits easily on a floppy disk! Most of this is a one-time cost. it doesn t mean that if you write twice as much code, it ll take up 1600KB. More likely, it ll take 810KB or something like that.

Don t worry about one-time startup costs.

The size usually results in static libraries being linked into your application.

You can reduce the size of the compiled binary by compiling to RELEASE versions, with optimizations to binary size.

Another source of executable size are the libraries. You said that you don t use external libraries, except for STD, so I believe you re including the C Runtime with your executable, ie, linking statically. so check for dynamic linking.

IMO you shouldn t really worry about that, but if you re really paranoid, check this: Smallest x86 ELF Hello World





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签