Why is my C++ executable so big? [duplicate]

2019-05-24 02:11发布

Possible Duplicate:
GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?

I've just started reading some C++ online tutorials and the first lesson was the Hello World program. When I compile the program to an executable, the size is over 400kb even though it's just a simple Hello World console program. Should it be this big? If not, why is it happening? Am I doing something wrong?

Here is the source:

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello World";
    cin.get();
}

Any help would really be appreciated. Thanks

标签: c++ size
5条回答
爷、活的狠高调
2楼-- · 2019-05-24 02:39

building in Debug mode will compile and link code that is not optimise in any way (speed/size) and you end up with bloated exec that includes debug version of system libs.

if you switch to release mode, the compiler and linker can be optimised to only use the functions they require in the final exec and also use their release versions. that way you will end up with 'hopefully' a smaller exec if you've selected optimise for space.

查看更多
该账号已被封号
3楼-- · 2019-05-24 02:41

Statically linking the C and/or C++ runtime can greatly increase the size. Also, compiling your program to include debugging information can increase the size.

查看更多
Luminary・发光体
4楼-- · 2019-05-24 02:46

Do not to link you app static. Try to make it dynamic.

查看更多
We Are One
5楼-- · 2019-05-24 02:50

This is almost certainly because you created a static executable, i.e. one which can run standalone and doesn't rely on run-time libraries. See for the documentation of your compiler/linker for how to avoid that.

Edit:

From your code I get 13540 bytes for a dynamically linked executable (gcc 4.3.2 on linux) but 6.7Mb for a statically linked executable.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-05-24 02:51

It probably is with debug information. If you strip that out (by building in release mode in Visual Studio, or using the strip command in Linux) it will be much smaller.

查看更多
登录 后发表回答