Visual Studio post version 6 has an interesting time with its runtime library. For those that remember back that far, Visual C++ 6 used a library called msvcrt.dll / msvcrtd.dll. These libraries are included, to this day, in all versions of Windows and remain perfect compatible with Visual C++ 6 compiled binaries.
A lot of other analysis has gone into this on other blogs. I won’t repeat all of it here, but be sure to check out the blog of the KovoIRC developer.
Let’s start out with a simple program. It’s quite basic, but it’s a good starting point. I have made a small Win32 Console Project in Visual Studio making sure that I have made a directory made for the solution and the project directory sits within it.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char* x = new char[100];
memcpy(x, "Hello World", 11);
puts(x);
delete [] x;
return 0;
}Your first question is probably; Why are you creating a buffer using new then using memcpy and delete? The answer is that I want to make sure I use the new/delete operators. sprintf isn’t available, so I can’t use that.

Visual Studio Runtime Picker
Let’s statically link the runtime with it and produce a release executable. That produces a 53KB EXE. Ok, that’s not fair, there’s features in other runtimes we can’t use. So let’s turn off the security checking etc. Still 53KB.

Normal Static Linked Binary Size
Now we have our base point. Let’s do the following:
- Convert the solution directory to a GIT repo
- Add the git submodule from git://github.com/leepa/libctiny.git
- Add the new sub project to the solution in Visual Studio
- Add a Project Dependency to the HelloBlogPost project so it depends on MiniCrt
Git makes managing these things insanely easy. By using a git submodule you get to ensure you stay at the point you want to, but update it easily if needs be. Basically, it’s a great way of including 3rd party libraries in a project.
To do all this we can do the following (assuming msysGIT in Windows).
git.exe init # You probably now want to do your # adds/commits and .gitignore # stuff git.exe submodule add -- "git://github.com/leepa/libctiny.git" "libctiny"
Ace, now let’s try and compile again… 4KB. That’s a bit better isn’t it? The same program but a much small runtime footprint.

EXE Size with libctiny
Isn’t that much better? For when you just need to do simple executable files, this can’t be beat. I can’t take credit for the library, I just added it a public Git repository and fixed a couple of new intrinsic functions that Visual Studio 2008 defines (ohama @ Google updated it to VS2005).
Credits: Under the Hood: Reduce EXE and DLL Size with LIBCTINY.LIB & omaha – Software installer and auto-updater for Windows.


No comments yet.