Search code examples
cwindowscompiler-optimizationfilesize

windows c compiler generating large executables


I was writing a c program for windows, and compiling it with: cl test.c and I noticed that the file size was 95744 bytes.

I thought that was strange so I began shortening the program to see if that would affect the size of the executable, but it stayed the same, I eventually just compiled an empty main function, with the same result, I attempted the same program on wsl2, compiling it with gcc test.c -o test and ran size on the elf which outputted:

   text    data     bss     dec     hex filename
   1224     544       8    1776     6f0 test

does anyone know why this is or how to make the output smaller?

any help is greatly appreciated.


Solution

  • The issue is that cl.exe is statically linking libc by default meaning the C runtime library will be embedded into your executable. That is where the bulk of the file size is coming from. The option /MD will do a dynamic linking so these functions can be found at runtime.

    Also, optimization options like /O2 will also help to reduce the size of the resulting executable.