windows - C++ executable size using MinGW -
i writing in c sizecoding demo-making competition (64kb) considering moving c++.
under mingw g++ have trouble .exe-size. (before using executable-packers, have down <100 kb.).
i have looked @ this: how reduce size of executable produced mingw g++ compiler? already using mingw/g++ 4.8.1 , -s -os ... see below (and 4.8.1 too: unrecognized option '-shared-libstdc++'
, cannot find -lstdc++_s
).
this little helloworld has 10 kb (which ok):
#include "windows.h" int main() { messageboxa(0, "test", "test", 0); return 0; }
however when add:
#include <string> ... std::string asdf;
it becomes 193 kb
and when add:
#include <iostream>
then becomes 756 kb.
i using these flags:
-std=c++11 -wall -s (or -wl,-s) -os -dndebug -fno-exceptions -fno-rtti (note: removed no effect)
there has way link use. missing?
optional 1: possible -shared-libstdc++ or -lstdc++_s working in mingw/g++ 4.8.1?
optional 2: when try -nostdlib
, replace main
winmain
:
#include "windows.h" int callback winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) { messageboxa(0, "test", "test", 0); return 0; }
i no compiler warnings runtime crash, works fine when compiling c though. (optional because don't want you/me spend time debugging crt-startup, compiler-linker-flag trim libraries more helpful)
those bytes "pulled in" standard library calls - high level ones tend bring below them along, memory allocation, exceptions use , on. easiest thing start minimize use. basing hello world on putchar() might give point of comparison. i'll focus on statically linked programs that's know, rubenvb's answer appears cover shared libraries anyway.
features new, delete, pure virtual functions etc. pull in bits of library , many dependencies underneath. quick overview on how replace here: http://ptspts.blogspot.com.au/2010/12/how-to-write-c-program-without-libstdc.html - if extreme, find version of malloc treated in same way.
more c++11 you'll run __cxa_guard_acquire , __cxa_guard_release if this:
int foo() { static int bar = 1; //thread safe in c++11, requires inbuilt functions }
for that, use -fno-threadsafe-statics flag if compiler supports it.
if doesn't quite far enough can link flag -map=filename.map on linux versions of ld generate "map" file. first section of file outputs lists each section pulled in, along 1 required it.*
*the map file reveal function-sections nothing standard library it's been compiled without flag, apart
Comments
Post a Comment