r/C_Programming • u/Suitable_Broccoli361 • May 31 '26
Very slow compiling time when including Windows.h
Hello guys I've been trying to figure out the problem for long I'm trying to use a Windows.h header file in my C code for some useful functions and when i compile the code with the Windows.h header included, it takes 10+ seconds.
I'm using a new version of MinGW and CodeBlocks IDE can someone help me please!
24
Upvotes
39
u/drmonkeysee May 31 '26
Right before the
#includetry#define WIN32_LEAN_AND_MEANwhich will exclude a lot of windows.h. Depending on what you’re using from the header this will probably speed things up (or fail to compile if what you’re using gets excluded by this macro).25
u/Humble_Response7338 May 31 '26
One alternative which i always use in my projects is to avoid any windows.h and define the function prototypes of th win32 api that I actually need.
This gives you very fast compile times.
Here is an example:
#ifndef _WINDOWS_
#define WIN32_API(r) __declspec(dllimport) r __stdcall
#define STD_OUTPUT_HANDLE ((unsigned long)-11)
WIN32_API(void *) GetStdHandle(unsigned long nStdHandle);
WIN32_API(int) WriteConsoleA(void *hConsoleOutput, void *lpBuffer, unsigned long nNumberOfCharsToWrite, unsigned long *lpNumberOfCharsWritten, void *lpReserved);
WIN32_API(char *) GetCommandLineA(void);
#endif /* _WINDOWS_ */
Here you can find an example of it which uses no windows.h and no C Standard library at all:
https://github.com/nickscha/nostdlib_templates
Hope this helps you.
It seems first quiet a lot of effort to define them on your own but I found that in most of projects I use very less (max 50 functions). Then you have the work once but you save a lot of compile times.
In my MSYS2 gcc/clang setup just avoiding windows.h for an empty file gets me from 0.3 seconds to 0.002 seconds.
4
u/ukaeh May 31 '26
You can also put windows.h in a precompiled header to speed things up
3
u/kun1z May 31 '26
This doesn't sound right as I have 3 different Windows compilers: Pelles C, gcc, and clang, and all of them compile instantly for small projects. I think this has more to do with MinGW and/or CodeBlocks than the windows.h header file.
4
u/QuirkyXoo Jun 01 '26
It's not windows.h but the compiler you use, also the WIN32_LEAN_AND_MEAN macro is mainly used to avoid compatibility conflicts, not just to reduce the compile time. I always used the native (MSVC) compiler and I never had a single compile time issue.
2
u/rickpo May 31 '26
Not sure how MinGW or CodeBlocks works, so this may be irrelevant. But is it possible you have something funky on your include path, like a network path? Are the windows headers at the end of the path?
2
u/timrprobocom Jun 01 '26
Windows.h expands into several hundred thousand lines of code.
1
u/howprice2 May 31 '26
To keep build times low, don't include system or standard library headers unless you absolutely need them, and try to never ever include them in your own header files - only ever include in .c files.
What are you using from Windows.h? Could you possibly roll your own equivalent?
You may be able to find compiler/linker options that profile your build.
1
u/Sqydev Jun 01 '26
I think the best way for this is to just make your own windows.h, yk, only the functions that you need. But also do you link the windows library or you use .a? I personally think that the problem is not windows.h but something other that you use when using windows.h, I’m sorry for being a bit stupid here but it’s 2:38 for me so