Hey all, I wanted to compare the size of the executables from VC++ and MASM32. I wrote a simple program that just displays a dialog box, and responds to a few user inputs, both in VC++ using C & win32, and MASM32. I made VC++ display an assembly listing, with optimization set to minimum size, and some other tweaks. The assembly file made by VC++ was actually better than my MASM32. But the VC++ exe file was huge compared to mine, ie 53kb to 7kb. Can someone tell me why?
Posted on 2001-05-04 22:36:00 by Magik
Magik, its the old rule, in assembler you get what you write, with most modern compilers you get more than you write. I have seen techniques with C, not C++ that will deliver EXE files in the size range of assembler files but it means no runtime library support and when you start adding the C runtime libraries, the size gets bigger faster. Another thing to check when you compare the sizes of the OBJ files before they are linked is if the C++ code is using the MSVCRT DLLs for their function calls, if so, they are just passing the calls to a DLL not compiling the code into the EXE file. Regards, hutch@pbq.com.au
Posted on 2001-05-04 23:00:00 by hutch--
I actually coded the VC code exactly as I would the MASM32 code, ie no run-time libraries, using only the Win32 API that were absolutely necessary. I know that people will say that in VC you must use WinMain(...), but actually, with optimizations turned on, the assembly listing does not contain this (well technically, it does have a single proc call, but that's it, no other overhead). Any plus, the assembly listing ouput from VC is more optimized that what I could do in raw MASM32.
Posted on 2001-05-05 00:52:00 by Magik
Make sure you're not creating a "debug" version. If you're creating a "release" version, the extra code is from a C library. A typical C++ startup library will have command line parsing and code to initialize static class objects.
Posted on 2001-05-05 01:19:00 by tank
Maybe its the startup code of the C program. VC++ (as well as other C++ compilers) need start up routines for their libraries. Also by default VC++ will include some libraries (even if you dont use them) if you dont specifically thell it not to do it.
Posted on 2001-05-05 03:04:00 by dxantos
yes, the entry point is not WinMain, but some function that is defined in some .lib that is linked as default together with your exe. 1. use release mode 2. under project>>settings there you have something like 'link with default library', you UN-check it 3. now you define in link options entry:your_function, and you erase SUBSYSTEM:WINDOWS 4. in .cpp you define you_functio, and you must get at least HINSTANCE...so use GetModuleHandle etc...then you call WinMain... 5. also look at ALIGN option in link options... this will chop down you exe tromendously!!, you can get even the same size as in MASM32!, just use all this stuff this is written from my head, but it gives you essential steps! have fun...
Posted on 2001-05-05 19:42:00 by Geecko
Thanx for the info, changing the options in VC++ did make a big difference, but still, the exe is nowhere near MASM size, actually its almost 5 times its size. is there anything else i can do in vc++ to make it even smaller?
Posted on 2001-05-08 10:35:00 by Magik
I was able to get my Uptime project down to 1kb in MASM and 36kb in VC++ (both Debug and Release). Basically, playing around with the settings can alter the size to your liking. There are dropdown menus on most tabs inside the settings pannel. It was a basic trial and error situation for me. Post your results if you do any better than 36kb. If you need further information, feel free to ask.
Posted on 2001-05-08 21:10:00 by usb
Magik, This is how I tested the minimum size of a C program in MSVC. Code a "main" function similar to the following.

void main()


      hInstance = GetModuleHandle(0);

In your build options,

/FILEALIGN:512 /entry:main
This builds a working window with an icon and menu at about 5k. Very close to what a MASM EXE will build for the same functionality. Regards, hutch@pbq.com.au
Posted on 2001-05-08 21:35:00 by hutch--
for a exe around 1kb, just do /ALIGN:16 /ENTRY:Your_Fuction
Posted on 2001-05-09 01:49:00 by Geecko
16-byte file alignment is a bad idea, it will not work on all versions of windows. 512-byte file alignment is the smallest that works everywhere. Anyway, you can easily get VC++ programs down to 4kb or such, but you need "/NODEFAULTLIBS" (or whatever that option is called), and you can of course not call any libc functions. Also, you need to define a couple of things that VC++ depends on, a variable (function?) called "fltused", and a weird-named startup function (you might be able to skip this if you use /entry). If you people want it, I can once-and-for-all post the necessary code and cmdline options, it's not very big.
Posted on 2001-05-09 03:55:00 by f0dder
WOW!! I got the file size below MASM32!! kewl eh. But that's not the whole truth, I got it below the MASM32 exe with no extra command line options to ml and link. With the same command line options, MASM32 also shrunk to the same teeny-weeny size of just 2,048bytes. I actually figured it out last night, reading the MSDN docs, but thanx for your replies everyone. By the way, this program was veerryy simple, I just did it to see how small i could get the exe file. It simply displays a dialog box and destroy's it when the user clicks the close button.
Posted on 2001-05-09 07:51:00 by Magik
Hey ! The smallest hello world example I ever compiled via VC6 was 1024 bytes big. You can get it with an small explanation on my homepage (www.y0da.cjb.net) under code snippets. The trick is the following... - define "msvcrt.dll" as standart library... "/MD" compiler option ("Project" / "Properities" / "C/C++" / "Code Generation") - merge all sections via the "MERGE" linker command - set the file alignment to 0x200 which is usually 0x1000 ("FILEALIGN") - skip a standart WinMain-EntryPoint by defining a new EntryPoint via the "ENTRY" linker command Maybe this helps someone...
Posted on 2001-05-09 17:14:00 by yoda