I am normally writing in C for sizecoding demo-making competition (64kb) but considering moving to C++.
Under MinGW g++ i have some trouble with the .exe-size. (Before using executable-packers, i have to get it down to <100 kb.).
I have looked at this:
How to reduce the size of executable produced by MinGW g++ compiler?
however i am already using MinGW/g++ 4.8.1 and -s -Os ... see below (and for 4.8.1 too: unrecognized option '-shared-libstdc++'
and cannot find -lstdc++_s
).
This little helloworld has only 10 kb (which is ok):
#include "windows.h"
int main() {
MessageBoxA(0, "test", "test", 0);
return 0;
}
However when i add:
#include <string>
...
std::string asdf;
it becomes 193 kb
And when i add:
#include <iostream>
then it becomes 756 kb.
I am using these flags:
-std=c++11
-Wall
-s (or -Wl,-s)
-Os
-DNDEBUG
-fno-exceptions
-fno-rtti
(note: removed those with no effect)
There has to be some way to only link what i use. What am i missing?
Optional 1: Is it possible to get -shared-libstdc++ or -lstdc++_s working in MinGW/g++ 4.8.1?
Optional 2: When I try -nostdlib
and replace main
with WinMain
:
#include "windows.h"
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
MessageBoxA(0, "test", "test", 0);
return 0;
}
I get no compiler warnings but a runtime crash, it works fine when compiling as C though. (optional because I don't want you/me to spend time debugging crt-startup, a compiler-linker-flag to trim libraries would be more helpful)