Marcus Marcus - 28 days ago 20
C Question

MinGW gcc vs linux gcc executable size

I wrote this small C program

#include <stdio.h>

int main() {
int i = 0;
while (i < 10){
printf("%i", i);
i++;
}

}


If I compile it with MinGW gcc the executable is 59kb but if I compile this with linux gcc, via the ubuntu on windows shell, the executable is only 9kb. 50kb seems like a lot of extra data... Why is this?

Answer

According to the MinGW wiki, debugging information can be included from the libraries linked with your executable. You can exclude the debugging information from your executable, with gcc "-s" option or the "strip" command.

Support for "Gnu style" or "MS style" printf format seems to be one of the differences between MinGW stdio and the GNU C library stdio.


MinGW

#>  gcc --version
gcc.exe (GCC) 5.3.0

#>  gcc -o printf_gcc_mingw.exe printf.c
#>  gcc -s -o printf_gcc_strip_mingw.exe printf.c
#>  du -ch *.exe
59K     printf_gcc_mingw.exe
45K     printf_gcc_strip_mingw.exe

MinGW-w64

#>  gcc -o printf_gcc.exe printf.c
#>  gcc -s -o printf_gcc_strip.exe printf.c
#>  gcc --version
gcc (tdm64-1) 5.1.0
#>  dir *.exe    
           132,613 printf_gcc.exe
            16,896 printf_gcc_strip.exe     
#>objdump -x printf_gcc_strip.exe 
    DLL Name: msvcrt.dll
    vma:  Hint/Ord Member-Name Bound-To
    85ca       55  __C_specific_handler
    85e2       78  __dllonexit
    85f0       81  __getmainargs
    8600       82  __initenv
    860c       83  __iob_func
    861a       91  __lconv_init
    862a       97  __set_app_type
    863c       99  __setusermatherr
    8650      116  _acmdln
    865a      123  _amsg_exit
    8668      141  _cexit
    8672      252  _fmode
    867c      330  _initterm
    8688      438  _lock
    8690      610  _onexit
    869a      820  _unlock
    86a4     1031  abort
    86ac     1049  calloc
    86b6     1062  exit
    86be     1081  fprintf
    86c8     1088  free
    86d0     1099  fwrite
    86da     1146  malloc
    86e4     1154  memcpy
    86ee     1163  printf
    86f8     1184  signal
    8702     1205  strlen
    870c     1208  strncmp
    8716     1240  vfprintf

Visual Studio 2015

#>  cl /MD printf.c /link  /out:printf_vs.exe
#>  dir *.exe    
             9,728 printf_vs.exe

#>printf_vs.exe
0123456789   
Comments