Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Windows Compiler for smallest executables

guys I want to start programing with C++. I have written some programs in vb6, vb.net and now I want to gain knowledge in C++, what I want is a compiler that can compile my code to the smallest windows application. For example there is a Basic language compiler called PureBasic that can make Hello world standalone app's size 5 kb, and simple socket program which i compiled was only 12kb (without any DLL-s and Runtime files). I know it is amazing, so I want something like this for C++.

If I am wrong and there is not such kind of windows compiler can someone give me a website or book that can teach me how to reduce C++ executable size, or how to use Windows API calls?

like image 896
Irakli Avatar asked Dec 10 '22 03:12

Irakli


2 Answers

I had to do this many years ago with VC6. It was necessary because the executable was going to be transmitted over the wire to a target computer, where it would run. Since it was likely to be sent over a modem connection, it needed to be as small as possible. To shrink the executable, I relied on two techniques:

  1. Do not use the C or C++ runtime. Tell the compiler not to link them in. Implement all necessary functionality using a subset of the Windows API that was guaranteed to be available on all versions of Windows at the time (98, Me, NT, 2000).
  2. Tell the linker to combine all code and data segments into one. I don't remember the switches for this and I don't know if it's still possible, especially with 64-bit executables.

The final executable size: ~2K

like image 131
Ferruccio Avatar answered Dec 23 '22 08:12

Ferruccio


Microsoft Visual C++ compiler for example... You just have to turn off linking to the C runtime (/NODEFAULTLIB) and your executable will be as small as 5KB. There's just one problem: you won't be able to use almost anything from the standard C and C++ library, nor standard features of C++ like exception handling, new and delete operators, floating point arithmetics, and more. You'll need to use only the pure OS features (e.g. create files with CreateFile, allocate memory with HeapAlloc, etc...).

like image 22
Yakov Galka Avatar answered Dec 23 '22 07:12

Yakov Galka