-
|
I have a question about filesizes when creating C++ code. I have a C++ project that uses some parts of the STL library quite a lot, particularly std::string, std::vectorstd::string and std::map<std::string, std::string>. The resulting filesize of the binary however is quite big with 15 MB. Even when adding the -mtiny flag, it only comes down to 14 MB. However, compiling the project natively with g++ for each platform results in a binary of 90k (Windows) to 200k (Linux, MacOS). Or did I overlook something to properly make C++ code created cosmoc++ smaller? Any idea what I could do to make it smaller in size? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
|
Use C. You nailed most of the things you can do to make C++ binaries smaller. Beyond that you can analyze what's being linked into your program. I have this command #!/bin/sh
nm -C --size "$@" |
sort -r |
grep ' [bBtTRr] ' |
exec lessThat'll hopefully give you a better idea of what sort of stuff the linker is pulling into the binary. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for that command. It won't work on APE binaries (naturally), and also not on stripped builds, but for unstripped builds (like the intermediate elf from cosmo or a direct gcc compile) it shows me what I expected - no external dependencies apart from my own code and the STL string/map/vector implementations. One thing I noticed though when comparing the cosmoc++ elf with the one generated by g++ directly: |
Beta Was this translation helpful? Give feedback.
-
|
Ok, thanks! As you can see, the same 8 (internal) functions are present (albeit in different order), but the section size is considerably larger on cosmoc++. I just want to know if there is anything sensible to do about this from my end or if I should just leave it as it is :) cosmoc++: g++: |
Beta Was this translation helpful? Give feedback.
-
|
Using further g++ compiler flags for disabling unwanted features I have now reduced the file size fom 1.8 MB to 1.3 MB, which is quite nice. :) Two more questions:
Thanks! |
Beta Was this translation helpful? Give feedback.
Just a quick note of my intermediate findings:
I was able to reduce the file size in tiny bits by replacing some of my internal functions, but the biggest reduction came when I added -fno-exceptions to the cosmoc++ call.
Since I don't use any exceptions myself in the code and don't catch any, I thought why not give it a try. And that parameter alone reduced the size by about 300kb :)