0

Suppose I have the following program:

#include <stdio.h>
int main()
{
    printf("This is a sample C program.\n");
    return 0;
}

If I compile it with the Microsoft compiler (cl.exe /O1 sample.c) on a Windows 7 32-bit machine, then it outputs an executable file that is 44 KB.

If I compile it with the GNU compiler (gcc sample.c) on a CentOS 64-bit machine, then it outputs an executable file that is 6 KB.

Generally speaking, why is there such a big difference in file size for this small program? Why does it take Windows 44 KB just to print a line and exit?

cm007
  • 1,352
  • 4
  • 20
  • 40

2 Answers2

2

If you use the /MD switch with cl.exe, it will dynamically link against msvcrt (the Microsoft C runtime library) and use the msvcrt.dll (and you will get a comparable executable size of 6KB), otherwise the code from the C library is statically linked into your executable increasing the size of the executable.

Your gcc compiler on CentOS is setup to dynamically link against the C library by default.

Nelson
  • 1,022
  • 5
  • 9
0

Apart from the links provided above, I feel this will also help you to understand on what happens when we compile using gcc!

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98