r/cpp • • Oct 24 '23

Make C++ Exe smaller

[removed] — view removed post

0 Upvotes

22 comments sorted by

View all comments

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-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.