Tutorial How to shave size off your app

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

λGhost

Newbie
Jun 4, 2014
4
144
0
This is a tutorial on how to reduce your app's size using the project property pages in Visual Studio 2012.

Here is a simple hello world app created with "Empty project" option, and default properties.

C++:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

const char g_szHelloWorld[] = "Hello Slim World";

int main()
{
   DWORD dwUnused;

   //display Hello World
   WriteFile(
      GetStdHandle(STD_OUTPUT_HANDLE),
      g_szHelloWorld,
      sizeof(g_szHelloWorld),
      &dwUnused,
      NULL
   );


   //pause screen
   ReadFile(
      GetStdHandle(STD_INPUT_HANDLE),
      &dwUnused,  //doesn't matter what buffer we use
      0,          //because we aren't writing anything to it
      &dwUnused,
      NULL
   );

   return 0;
}
It compiles to 30.5 KB.

First thing I usually do is go to the configuration manager. From the project Property Pages -> Configuration Manager..., set Active solution configuration from Debug to Release and click close.
That alone shaves off 23.5 KB leaving 7 KB.

However, the CRT is dynamically linked in, so its size is not reflected on the exe. Which can be great if you don't mind redistributing the crt dll with your app. If you're like me, you prefer apps that work out of the box without installing anything else. So lets remove the CRT as well.

To remove the CRT we have to add it to the exe itself, ironic isn'it it. Go back into the project Property Pages -> C/C++ -> Code Generation and set Runtime Library to Multi-threaded (/MT). This statically links the CRT to the exe. Rebuild it, and now we're at 55.5 KB. The CRT added 48.5 KB.

Now we will really remove it this time with some tweaks. Go back into the project Property Pages and set the following:
C/C++ -> Code Generation -> Security Check No (/GS-)
C/C++ -> Language -> Enable Run-Time Type Information No (/GR-)
Linker -> Input -> Addtional Dependencies kernel32.lib
Linker -> Input -> Ignore All Default Libraries Yes (/NODEFAULTLIB)
Linker -> Advanced -> Entry Point main

Compile it again, and it's down to 3 KB. We shaved 52.5 KB by removing the CRT, wow! But we can go even further by adjusting even more properties:
C/C++ -> Optimization -> Optimization Full Optimization (/Ox)
C/C++ -> Optimization -> Omit Frame Pointers Yes (/Oy)
Linker -> Debugging -> Generate Debug Info No
Linker -> Manifest File -> Generate Manifest No (/MANIFEST:NO)
Linker -> Advanced -> Randomized Base Address No (/DYNAMICBASE:NO)
Linker -> Advanced -> Fixed Base Address Yes (/FIXED)
Linker -> Advanced -> Merge Sections .rdata=.text

Which brings us down to 1.00 KB!

Removing the CRT will often mean that you will have to use your own code for basic functions such as new, and delete.
Below is the Solution I used in this tutorial.


λGhost
 
Last edited by a moderator:

Heisa

Jr.Coder
Full Member
Nobleman
Jul 3, 2013
77
703
3
Please use code tags the next time, so it's more readable. Good work though.
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods