Using Glibc in Windows

Hi everyone,

I was recently trying out compiling swift on windows, and I found that Glibc was not something that could be imported, or at least not without some extra doing. I was trying to use the sleep() function that glibc provides.

My question is simple: how can I import glibc, or something equivalent?

Glibc is not available on windows as it wraps the system library of the same name. The system libc on windows, at least for modern cases, is ucrt. However, the windows C library does not provide a sleep function. The closest thing would be Sleep from the Windows SDK. This has differences in semantics that it limits you to 10ms granularity. This would be available through the WinSDK module as it is part of the SDK. The other option would be to use Foundation which provides Thread.sleep(forTimeInterval:). For a longer term solution, I think that this would be a great thing to add to the new System package.

4 Likes

So does that mean on Windows I can import ucrt and then use functions like fputs?

Correct, that should work just fine. CRT Alphabetical Function Reference | Microsoft Learn should be available for use after you import the ucrt module.

2 Likes

Some CRT functions seems missing like _kbhit(). I used _kbhit() frequently and to access the function I must manually add the missing conio.h in ucrt.modulemap on each Swift Windows release.

Please Swift Windows team add conio.h to ucrt.modulemap in the current Swift Windows distrib.

Thanks !

Patches welcome :slight_smile: However, would you please file an issue? I don't mind taking care of this after the rebranch work is taken care of.

1 Like