Size of Win32 and Console applications. 
Author Message
 Size of Win32 and Console applications.

Building a console application with the following code:
    int main()
   {
        return 0;
    }

The size of the resulting executable is ~20kb (in the default release mode)
Similarly a skeleton win32 application has about the same size.
Why do these exe's have to be 20,000 bytes when almost all the code,
resources and data are located in DLL's(User, Kernel and GDI)?

In the case of a console application, initially I thought that the console
creation code might be statically allocating space for the screen buffer.
But the line:-
      Size of uninitialized data    00000000
in the exe header indicates otherwise. Further there is an API
function which allows you to change the size of the buffer.
Also the size of the skeleton Win32 app and skeleton console
app are about the same, which seems to imply (as expected)
that the size of the code, within the exe, related to creating and
displaying text, buttons icons, etc is quite small.

So what else is contained in these executables?

Thanks.

Regards,
S. K. Mody.



Sun, 26 May 2002 03:00:00 GMT  
 Size of Win32 and Console applications.

"S.K.Mody" > Building a console application with the following code:

Quote:
>     int main()
>    {
>         return 0;
>     }

> The size of the resulting executable is ~20kb (in the default release
mode)
> Similarly a skeleton win32 application has about the same size.
> Why do these exe's have to be 20,000 bytes when almost all the code,
> resources and data are located in DLL's(User, Kernel and GDI)?

Two basic things:
1- The CRT is statically linked by default, which adds a lot of code to your
app.
2- The default  alignment of sections in the resulting .exe is somewhat
large. I believe current link.exe versions use a 4KB alignment by default.

You can go around both issues. For the first, you can aoid linking in the
CRT, and link to the dll version of it, or avoid it altogether (see the
/NODEFAULTLIB linker option). In that case, you'd have to provide suitable
initialization code yourself.

For the second, you can adjust section alignment to a smaller value, say,
512, via the /ALIGN linker switch.

--
Tomas Restrepo
http://members.xoom.com/trestrep/



Sun, 26 May 2002 03:00:00 GMT  
 Size of Win32 and Console applications.
Thanks for your replies. I found an article "Under the hood" by Matt Pietrek
in the MSDN CD which has a nice discussion of this. From what I understand,
apart from the utility functions, the C runtime lib contains code to 1.
Process
the command line string. 2. Initialize/Clean up the heap 3. Call static
constructors
& destructors. 4. Do Input and Output. 5.Handle exceptions.

1 and 2 are used in the initialization routine _mainCRTStartup contained in
the source file crt0.c. However I couldn't find the minimal #include switch
that you mentioned, though I could leave CRT out altogether using the Zl
compiler option. I could do this if I was not using I/O and new/delete, etc.
(and if no associated DLL is using new/delete) and replace the command
line processing code with my own (in a routine called _mainCRTStartup
which is assumed by the MS linker to be the name of the console app's
entry point.)

I used the MD compiler option to link the CRT as a DLL, to see what
the sizes of the exe's looked like:_
     statically linked      DLL
Console    21 kb       2.5 kb (Header + 0.5 kb code + 1KB data) (3 sections)
GUI          23kb        3.5 kb (Header + 1 kb + 1.5 kb) (3 sections)

Quite dramatic (in release mode optimized for speed).
I'm still not sure what occupies most of this space. Opening it as
a text file, there seem to be some DLL names, function names etc.
Presumably the OS uses these to decide how to load the DLL's
involved. The rest of the space seems to be occupied by the header.

With the code below (compiled with the Zl option)
the exe size is even smaller:-
   1.5 kb (Header + 0.5 kb + 0.5 kb) (2 sections).

#include <windows.h>
void __cdecl mainCRTStartup();
int main();

void __cdecl mainCRTStartup() { ////do nothing initialization routine.
  int ret = main();
  ExitProcess(ret);

Quote:
}

int main()
{
  return 0;

Quote:
}

I wonder how you can create a Win32 App which is
neither a GUI nor a console app - a program which does not involve
linking to any other code or DLL.(except possibly for Kernel32.dll)

Thanks for your help.
Regards,
S. K. Mody.


Quote:
> the CRT's are pulled in by default as a static lib - If you don't need
> them - you can have a minimail include of the CRT's there's a #define that
> does this for you  - try looking in the CRT sources



> > Building a console application with the following code:
> >     int main()
> >    {
> >         return 0;
> >     }

> > The size of the resulting executable is ~20kb (in the default release
> mode)
> > Similarly a skeleton win32 application has about the same size.
> > Why do these exe's have to be 20,000 bytes when almost all the code,
> > resources and data are located in DLL's(User, Kernel and GDI)?

> > In the case of a console application, initially I thought that the
console
> > creation code might be statically allocating space for the screen
buffer.
> > But the line:-
> >       Size of uninitialized data    00000000
> > in the exe header indicates otherwise. Further there is an API
> > function which allows you to change the size of the buffer.
> > Also the size of the skeleton Win32 app and skeleton console
> > app are about the same, which seems to imply (as expected)
> > that the size of the code, within the exe, related to creating and
> > displaying text, buttons icons, etc is quite small.

> > So what else is contained in these executables?

> > Thanks.




- Show quoted text -

Quote:
> [cut]
> Two basic things:
> 1- The CRT is statically linked by default, which adds a lot of code to
your
> app.
> 2- The default  alignment of sections in the resulting .exe is somewhat
> large. I believe current link.exe versions use a 4KB alignment by default.

> You can go around both issues. For the first, you can aoid linking in the
> CRT, and link to the dll version of it, or avoid it altogether (see the
> /NODEFAULTLIB linker option). In that case, you'd have to provide suitable
> initialization code yourself.

> For the second, you can adjust section alignment to a smaller value, say,
> 512, via the /ALIGN linker switch.

> --
> Tomas Restrepo
> http://members.xoom.com/trestrep/



Tue, 28 May 2002 03:00:00 GMT  
 Size of Win32 and Console applications.
S.k.,
 Thanks for your replies. I found an article "Under the hood" by Matt
Pietrek

Quote:
> in the MSDN CD which has a nice discussion of this. From what I
understand,
> apart from the utility functions, the C runtime lib contains code to 1.
> Process
> the command line string. 2. Initialize/Clean up the heap 3. Call static
> constructors
> & destructors. 4. Do Input and Output. 5.Handle exceptions.

> 1 and 2 are used in the initialization routine _mainCRTStartup contained
in
> the source file crt0.c. However I couldn't find the minimal #include
switch
> that you mentioned, though I could leave CRT out altogether using the Zl
> compiler option. I could do this if I was not using I/O and new/delete,
etc.
> (and if no associated DLL is using new/delete) and replace the command
> line processing code with my own (in a routine called _mainCRTStartup
> which is assumed by the MS linker to be the name of the console app's
> entry point.)

> I used the MD compiler option to link the CRT as a DLL, to see what
> the sizes of the exe's looked like:_
>      statically linked      DLL
> Console    21 kb       2.5 kb (Header + 0.5 kb code + 1KB data) (3
sections)
> GUI          23kb        3.5 kb (Header + 1 kb + 1.5 kb) (3 sections)

> Quite dramatic (in release mode optimized for speed).
> I'm still not sure what occupies most of this space. Opening it as
> a text file, there seem to be some DLL names, function names etc.
> Presumably the OS uses these to decide how to load the DLL's
> involved. The rest of the space seems to be occupied by the header.

The PE headers are somewhat large, but not that much. However, you have to
count in the spoace ocoupied by the other sections, like the Import address
table. Add to the mix a large section alignment and there you go. If you'd
like to see it in full{*filter*}detail, look at the file with dumpbin.exe.

- Show quoted text -

Quote:

> With the code below (compiled with the Zl option)
> the exe size is even smaller:-
>    1.5 kb (Header + 0.5 kb + 0.5 kb) (2 sections).

> #include <windows.h>
> void __cdecl mainCRTStartup();
> int main();

> void __cdecl mainCRTStartup() { ////do nothing initialization routine.
>   int ret = main();
>   ExitProcess(ret);
> }

> int main()
> {
>   return 0;
> }

> I wonder how you can create a Win32 App which is
> neither a GUI nor a console app - a program which does not involve
> linking to any other code or DLL.(except possibly for Kernel32.dll)

That's up to you to decide: Just avoid the CRT, and explicitly leave only
kernel32.lib in the list of linking libraries. However, notice that you
still have to mark the app as a console or gui app. It's no big deal,
because that simply informs the loader if it has to create a console or not,
and doesn't imply linking to a specifi library.

Now, on NT, you'll bring ntdll.dll wether you like it or not. Also,
technically you could create an app on NT that was only dependant on
ntdll.dll, by creating a native app, but that's a lot more work, and useless
because the user-mode image loader doesn't know how to load them.

--
Tomas Restrepo
http://www.*-*-*.com/



Tue, 28 May 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how to use a console window in a win32 application..-- not console application

2. minimizing size of Win32 Console Application

3. Win32 specific: Calling modal dialog box from a console application

4. #include statement compiler error and Win32 Simple Console Application

5. Icon in a Win32 console application

6. Iconized C++ Win32 console application.

7. How to Validate data in a Win32 Console Application

8. WIN32 Console Application Issues

9. Overriding CAsyncSocket OnReceive doesn't work in Win32 Console Application

10. Disable Range Checking for Integers for Visual C++ Win32 Console Applications

11. win32 console application with german "umlaut"

12. Screen Control in Win32 Console Applications

 

 
Powered by phpBB® Forum Software