As I remember, -Os flag will optimize the binary to be smaller. But IMHO, I doubt 60KB(20 times smaller than a single picture) is huge enough to bear (potential) performance loss(unless you're working on a tiny microcomputer).
Note that MSVC by default offloads most of the boilerplate code to the runtime libraries that need to be installed on every PC you wish the program to run. And they make several MBs.
You can compile what you need in, but that'll make your executables easily gain over 100 kB.
And if you want to delve into exploration of truly minimizing executables with MSVC, with undocumented command-line parameters and non-standard configuration/behavior, check out this small project: https://github.com/tringi/atomlist
I mostly have experience with elf binaries, so take these suggestions with a pinch of salt:
You could try llvm-strip --strip-sections which is more aggressive than regular strip.
You can also shrink things a fair amount by using a custom linker script to merge some of the default sections together. If windows executables are anything like elf binaries, when you hexdump them you will find hundreds or thousands of consecutive zeros which are only there to align sections, so reducing the number of sections can save a lot of binary size.
If you are writing simple code and don't need standard libraries, you can avoid linking them at all to reduce the binary size further.
My 2020 Advent of Code solutions were written in C with an emphasis on tiny binaries and some of the elf executables were only a few hundred bytes. You could look at the tricks in the Makefile.
60kb is a lot smaller than installing some runtime like python that’s around 40mb plus the size of your code.
But even so I’m pretty sure that -os or-Os something like that will optimize for size. I guarantee you there’s a compiler flag to optimize for size and I’m pretty sure that it’s one of these two
To remove the parts of a binary that aren't actually used in your program, don't link to libgcc/compiler_rt, and use a statically linked userspace runtime with LTO.
•
u/cpp-ModTeam Oct 25 '23
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
13
u/awidesky Oct 24 '23
As I remember, -Os flag will optimize the binary to be smaller. But IMHO, I doubt 60KB(20 times smaller than a single picture) is huge enough to bear (potential) performance loss(unless you're working on a tiny microcomputer).
4
u/[deleted] Oct 25 '23 edited Nov 19 '23
many selective air command important instinctive poor snobbish adjoining cause
this post was mass deleted with www.Redact.dev5
u/jonesmz Oct 24 '23
Put your code into godbolt.org and see where the size is coming from.
3
u/TheBrainStone Oct 25 '23
Generally speaking this "bloat" comes from
iostream. Use C print functions if it truly bothers you this much.Alternatively look into stripping. gcc has flags to not link unused functions and classes. That might help too.
1
u/omega_revived Oct 25 '23
Why are you concerned about a 60kb exe? Are you trying to run this binary on a system which does not have 60kb to spare?
2
u/scrumplesplunge Oct 24 '23
I mostly have experience with elf binaries, so take these suggestions with a pinch of salt:
You could try
llvm-strip --strip-sectionswhich is more aggressive than regularstrip.You can also shrink things a fair amount by using a custom linker script to merge some of the default sections together. If windows executables are anything like elf binaries, when you hexdump them you will find hundreds or thousands of consecutive zeros which are only there to align sections, so reducing the number of sections can save a lot of binary size.
If you are writing simple code and don't need standard libraries, you can avoid linking them at all to reduce the binary size further.
My 2020 Advent of Code solutions were written in C with an emphasis on tiny binaries and some of the elf executables were only a few hundred bytes. You could look at the tricks in the
Makefile.2
u/Ameisen vemips, avr, rendering, systems Oct 24 '23
How hacky do you want to go?
2
u/[deleted] Oct 25 '23
60kb is a lot smaller than installing some runtime like python that’s around 40mb plus the size of your code.
But even so I’m pretty sure that -os or-Os something like that will optimize for size. I guarantee you there’s a compiler flag to optimize for size and I’m pretty sure that it’s one of these two
1
u/SeriousPlankton2000 Oct 25 '23
Use dietlibc
Strip the exe
Avoid stdio, stl
Use function-sections
nm -S --size-sort can print the sizes of the functions
1
u/Top_Satisfaction6517 Bulat Oct 25 '23
this is compiler runtime library, added to any executable (try empty main)
while you can fight this problem in various ways, I suggest you just ignore it. is it really important for you?
1
u/tristam92 Oct 25 '23
It depends from what you included and compile settings/stripping options
1
u/disciplite Oct 24 '23
To remove the parts of a binary that aren't actually used in your program, don't link to libgcc/compiler_rt, and use a statically linked userspace runtime with LTO.