This post is basically an effort to know what happens when we compile a simple Hello World program on Windows. In this case, I've used Visual Studio 2005.
First I've written a small usual Hello World program with all default settings provided by Visual Studio 2005.
The program looks like below:
int main(void)
{
printf("Hello World build on VS 2005--Default\n");
return 0;
}
The program compiled against the default C-runtime library. No changes have been done in any settings. I've built it in Debug as well as in release mode. In Debug build, the program size is 40 KB. In release build: Its size is 6 KB.
Now I've written another Hello World Program but switched off the default C-Runtime library. Rather, I've used the standard Windows library and provided definitions of functions like printf as well as the CRT start-up function. I've also switched off Buffer Security Check and Basic Runtime checks set to Default. The last two settings are enabled to avoid linking issues since I'm not going to use the standard C runtime library.
So the code looks like the below:
extern "C" int __cdecl printf(const char * format, ...)
{
char szBuff[1024];
int retValue;
DWORD cbWritten;
va_list argptr;
va_start( argptr, format );
retValue = wvsprintfA( szBuff, format, argptr );
va_end( argptr );
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), szBuff, retValue, &cbWritten, 0 );
return retValue;
}
int main(void)
{
printf ("Hello World build on VS 2005\n");
return 0;
}
int mainCRTStartup() // This is my CRT start-up finction which in turn invokes the main
{
int retval;
retval = main();
ExitProcess(retval);
}
I've built this in Debug as well as in release mode. And amazingly the size was reduced to 24 KB in debug (16 KB less than the default one) and 3 KB in release (3 KB less than the default one as mentioned before). However, both program shows the same output.
This is one of the things Matt Pietrek has shown in his well-known article "Under the Hood" (http://msdn.microsoft.com/library/bb985746.aspx).
Note: The printf function has been adapted from Matt's article.
First I've written a small usual Hello World program with all default settings provided by Visual Studio 2005.
The program looks like below:
int main(void)
{
printf("Hello World build on VS 2005--Default\n");
return 0;
}
The program compiled against the default C-runtime library. No changes have been done in any settings. I've built it in Debug as well as in release mode. In Debug build, the program size is 40 KB. In release build: Its size is 6 KB.
Now I've written another Hello World Program but switched off the default C-Runtime library. Rather, I've used the standard Windows library and provided definitions of functions like printf as well as the CRT start-up function. I've also switched off Buffer Security Check and Basic Runtime checks set to Default. The last two settings are enabled to avoid linking issues since I'm not going to use the standard C runtime library.
So the code looks like the below:
extern "C" int __cdecl printf(const char * format, ...)
{
char szBuff[1024];
int retValue;
DWORD cbWritten;
va_list argptr;
va_start( argptr, format );
retValue = wvsprintfA( szBuff, format, argptr );
va_end( argptr );
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), szBuff, retValue, &cbWritten, 0 );
return retValue;
}
int main(void)
{
printf ("Hello World build on VS 2005\n");
return 0;
}
int mainCRTStartup() // This is my CRT start-up finction which in turn invokes the main
{
int retval;
retval = main();
ExitProcess(retval);
}
I've built this in Debug as well as in release mode. And amazingly the size was reduced to 24 KB in debug (16 KB less than the default one) and 3 KB in release (3 KB less than the default one as mentioned before). However, both program shows the same output.
This is one of the things Matt Pietrek has shown in his well-known article "Under the Hood" (http://msdn.microsoft.com/library/bb985746.aspx).
Note: The printf function has been adapted from Matt's article.
Comments