With mingw-w64 headers, including windows.h ends up including stdlib.h and malloc.h implicitly, with the following path:
compiler-rt/lib/asan/asan_malloc_win.cc
x86_64-w64-mingw32/include/windows.h
x86_64-w64-mingw32/include/windef.h
x86_64-w64-mingw32/include/minwindef.h
x86_64-w64-mingw32/include/winnt.h
lib/clang/8.0.0/include/x86intrin.h
lib/clang/8.0.0/include/immintrin.h
lib/clang/8.0.0/include/xmmintrin.h
lib/clang/8.0.0/include/mm_malloc.h
x86_64-w64-mingw32/include/c++/v1/stdlib.h
x86_64-w64-mingw32/include/stdlib.h
These headers include declarations of functions like e.g. free(), without the dllexport attribute, but other included headers also include inline functions that reference free().
If these functions have been declared before (either with or without dllimport), we can still redeclare them with dllexport, this only yields a warning. However, if they have been declared without dllimport and referenced by an inline function in the headers, it has already been emitted to IR and we can't add the dllexport attribute afterwards.
By undefining STDC_HOSTED, we avoid xmmintrin.h including mm_malloc.h.
How badly do we need this include? It looks like we only get it for a few typedefs like HANDLE, LPVOID, and some other stuff that we could avoid without too much work. This file is intentionally isolated to avoid these kinds of conflicts. Should we just isolate it from windows.h too?