Feb 17, 2025 08:25 AM
(This post was last modified: Feb 17, 2025 11:18 AM by WhiteTV.)
Generally in todays world you usually don't care anymore how big your compiled binary is. But in our domain, especially when it comes to Red Teaming and Malware development, you want the smallest possible filesize.
Apart from optimized code, without external dependencies (looking at you OpenSSL with 5MB of size), you can optimize your Compile settings for reduced size.
The following Visual Studio (2022) Settings are the ones I use. Compared to the default settings, these reduced the statically linked binary size in my case from 480kb to 300kb.
Note that the following settings are for WinApi applications. If you want a console window, skip the SubSystem: Windows (/SUBSYSTEM:WINDOWS) setting
Also some settings might not be available depending which VS or C++ versions you are using.
Project Properties -> C/C++ -> Optimization:
Project Properties -> C/C++ -> Advanced:
Edit: To further analyze your binary, SizeBench from Windows can be used to exactly show you which libraries are taking up a lot of space in your compiled Binary. https://github.com/microsoft/SizeBench
Apart from optimized code, without external dependencies (looking at you OpenSSL with 5MB of size), you can optimize your Compile settings for reduced size.
The following Visual Studio (2022) Settings are the ones I use. Compared to the default settings, these reduced the statically linked binary size in my case from 480kb to 300kb.
Note that the following settings are for WinApi applications. If you want a console window, skip the SubSystem: Windows (/SUBSYSTEM:WINDOWS) setting
Also some settings might not be available depending which VS or C++ versions you are using.
Project Properties -> C/C++ -> Optimization:
- Optimization: Maximum Optimization (Favor Size) (/O1)
- Favor Size or Speed: Favor Small Code (/Os)
- Whole Program Optimization: Yes (/GL)
- Enable Function-Level Linking: Yes (/Gy)
- Omit Frame Pointers: Yes (/Oy)
- Enable String Pooling: Yes (/GF)
- Runtime Library: Multi-threaded (/MT) for static linking
- Buffer Security Check: No (/GS-)
- Control Flow Guard: No
- Add: NDEBUG;_RELEASE
- Project Properties -> Linker -> Optimization:
- Link Time Code Generation: Use Link Time Code Generation (/LTCG)
- References: Eliminate Unreferenced Data (/OPT:REF)
- Enable COMDAT Folding: Remove Redundant COMDATs (/OPT:ICF)
- Generate Debug Info: No
- Strip Private Symbols: Yes
- SubSystem: Windows (/SUBSYSTEM:WINDOWS)
- Enable Large Addresses: No
Code
/EMITPOGOPHASEINFO:NO /INCREMENTAL:NO /RELEASEProject Properties -> C/C++ -> Advanced:
- Disable Specific Warnings: 4711;4710 (to disable inline expansion warnings)
- Inline Function Expansion: Only __inline (/Ob1)
- Debug Information Format: None
Code
strip "$(TargetPath)"Edit: To further analyze your binary, SizeBench from Windows can be used to exactly show you which libraries are taking up a lot of space in your compiled Binary. https://github.com/microsoft/SizeBench

