Reducing GCC target EXE code size?

走远了吗. 提交于 2019-12-05 08:36:46

This isn't a particularly meaningful thing to do. It might be possible to eliminate a few things, but as soon as you have a program that actually does anything then it'll pull those things straight back in again.

For example, on another platform (I don't do much Windows stuff), the minimum size for a program is larger than you would think because every program has an atexit handler to clean up. That handler has a possible error case which means it pull in printf and all the I/O stuff. Atexit itself also pulls in malloc and all the memory handing stuff. And no doubt there's a few other bits besides. The end result is a 400KB static binary size. That's annoying in a no-op program, but in reality all programs would need this stuff, so it's a moot point.

In general, if you want to minimise program size, compile with -Os, and try to use -flto or -fwhole-program (but this last will need lots of changes to your build procedure). Also, don't use -g, and do strip the final binaries (if that doesn't break them).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!