Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

VC++ Question

I was wondering if anyone knows why when I complile a program, my VC++ 6 standard edition adds around 10 to 20k of code? (I decompiled a couple of small programs I made and found the extra code) Anything that was added seems to be whats used when the application starts.



Does anyone know what this code does, weather or its even necessary, and if not, if/how it can be turned off?



Thanks in advance,

Justin


Comments

  • This is the stub code VC++ adds to your code to initialize the C Run Time library, you can get rid of it easy, but be aware, if you do, you wont be able to call any C functions anymore in your code, only APIs.



    // main.c

    #include



    void __cdecl WinMainCRTStartup( void )

    {

    int ret;



    ret = WinMain( GetModuleHandle( NULL ), NULL, NULL, SW_SHOWDEFAULT );



    ExitProcess( ret );

    }



    int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR pCmdLine, int nCmdShow )

    {

    MessageBox( NULL, "Test", "Test", 0 );



    return 0;

    }



    This will produce a 2048 byte EXE in release mode.





    : I was wondering if anyone knows why when I complile a program, my VC++ 6 standard edition adds around 10 to 20k of code? (I decompiled a couple of small programs I made and found the extra code) Anything that was added seems to be whats used when the application starts.

    :

    : Does anyone know what this code does, weather or its even necessary, and if not, if/how it can be turned off?

    :

    : Thanks in advance,

    : Justin

    :






  • Thanks for your reply, but now I have a couple of new questions.



    The first was; is there a way to disable the stub code when I'm using .cpp files?



    The other was; when I used this code in a .c file, it only reduced the EXE's file size from 24 to 16k. Could this just be because I only have the standard edition of VC++ 6, or was there something else I should have done?





    : This is the stub code VC++ adds to your code to initialize the C Run Time library, you can get rid of it easy, but be aware, if you do, you wont be able to call any C functions anymore in your code, only APIs.

    :

    : // main.c

    : #include

    :

    : void __cdecl WinMainCRTStartup( void )

    : {

    : int ret;

    :

    : ret = WinMain( GetModuleHandle( NULL ), NULL, NULL, SW_SHOWDEFAULT );

    :

    : ExitProcess( ret );

    : }

    :

    : int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR pCmdLine, int nCmdShow )

    : {

    : MessageBox( NULL, "Test", "Test", 0 );

    :

    : return 0;

    : }

    :

    : This will produce a 2048 byte EXE in release mode.


  • An addition to my last post: When I compiled the code you gave me in a .c file in release mode, then decompiled the program, the resulting assembly only contained the code that I expected, and amounted to 67 bytes. What could VC++ still be adding to result in a 16k program?



    : Thanks for your reply, but now I have a couple of new questions.

    :

    : The first was; is there a way to disable the stub code when I'm using .cpp files?

    :

    : The other was; when I used this code in a .c file, it only reduced the EXE's file size from 24 to 16k. Could this just be because I only have the standard edition of VC++ 6, or was there something else I should have done?

    :

    :

    : : This is the stub code VC++ adds to your code to initialize the C Run Time library, you can get rid of it easy, but be aware, if you do, you wont be able to call any C functions anymore in your code, only APIs.

    : :

    : : // main.c

    : : #include

    : :

    : : void __cdecl WinMainCRTStartup( void )

    : : {

    : : int ret;

    : :

    : : ret = WinMain( GetModuleHandle( NULL ), NULL, NULL, SW_SHOWDEFAULT );

    : :

    : : ExitProcess( ret );

    : : }

    : :

    : : int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR pCmdLine, int nCmdShow )

    : : {

    : : MessageBox( NULL, "Test", "Test", 0 );

    : :

    : : return 0;

    : : }

    : :

    : : This will produce a 2048 byte EXE in release mode.

    :






  • OK, I'm using VC++ 5.0 so I think 6 has different default options. Anyway, I managed to change the size of that program to 752 bytes by adding this to the linker options

    /ALIGN:16

    16 is the minimum number of bytes the compiler allowed me, I think the name of this flag explians it all.



    Hope it works



    : An addition to my last post: When I compiled the code you gave me in a .c file in release mode, then decompiled the program, the resulting assembly only contained the code that I expected, and amounted to 67 bytes. What could VC++ still be adding to result in a 16k program?

    :

    : : Thanks for your reply, but now I have a couple of new questions.

    : :

    : : The first was; is there a way to disable the stub code when I'm using .cpp files?

    : :

    : : The other was; when I used this code in a .c file, it only reduced the EXE's file size from 24 to 16k. Could this just be because I only have the standard edition of VC++ 6, or was there something else I should have done?

    : :

    : :

    : : : This is the stub code VC++ adds to your code to initialize the C Run Time library, you can get rid of it easy, but be aware, if you do, you wont be able to call any C functions anymore in your code, only APIs.

    : : :

    : : : // main.c

    : : : #include

    : : :

    : : : void __cdecl WinMainCRTStartup( void )

    : : : {

    : : : int ret;

    : : :

    : : : ret = WinMain( GetModuleHandle( NULL ), NULL, NULL, SW_SHOWDEFAULT );

    : : :

    : : : ExitProcess( ret );

    : : : }

    : : :

    : : : int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR pCmdLine, int nCmdShow )

    : : : {

    : : : MessageBox( NULL, "Test", "Test", 0 );

    : : :

    : : : return 0;

    : : : }

    : : :

    : : : This will produce a 2048 byte EXE in release mode.

    : :

    :

    :

    :






  • Thanks for your replies. I wasn't able to get the EXE all the way down to 752 bytes, but I did get it down pretty far. When I used 16 as the value for the /ALIGN option, the program wouldn't run but, I kept changing the number, when I used 4096 (go figure why this number works :), I could get the program to run & it only compiled at 2.5k



    I also figured out how to get the stub code removed with .cpp files. When I used the /entry option it would compile without it, and I didn't even need to make my own WinMainCRTStartup function. (I'm not sure I can get away with using it that often though, not many programs that I know of can go without the C runtimes very well :)



    thanks again,

    Justin



    : OK, I'm using VC++ 5.0 so I think 6 has different default options. Anyway, I managed to change the size of that program to 752 bytes by adding this to the linker options

    : /ALIGN:16

    : 16 is the minimum number of bytes the compiler allowed me, I think the name of this flag explians it all.

    :

    : Hope it works


Sign In or Register to comment.