cgccexecutableelfcl

Difference between Microsoft compiler and GNU compiler, in terms of output executable file size


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?


Solution

  • 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.