zig version 0.15.2. Build the following code with zig build-exe -target x86_64-windows-gnu main.zig fails. Error message is dependency on libc must be explicitly specified in the build command.
Adding -lc after zig build-exe fixes the compilation error. Removing the posix.recv line without adding -lc also fixes the error.
I’m quite confused. I assume the socket family functions are from ws2_32.dll. Is ws2_32 part of libc on Windows? Why does posix.recv require libc while posix.socket doesn’t? Thanks for any help!
many posix functions require libc on windows, I can only guess why that is.
my guess is adapting windows apis to a posix interface is a significant amount of work, work that its libc already does. Zig only provides its own implementation for the easier or more needed (by the compiler) apis.
std.posix.recv indirectly calls std.c.recvfrom, which seems to come from mingw-w64-crt and depends on bunch of mingw libraries including libmingw32. libmingw32 is considered libc by zig, so zig reports libc is required. The system ws2_32.dll is not part of libc. So if I don’t want to link against libc, I should write my own wrapper recvfrom which calls std.os.windows.ws2_32.recvfrom.
std.posix seems not very usable for Windows socket programming right now. posix.close calls CloseHandle, while for a SOCKET fd it should be closesocket. posix.sendto uses ws2_32 while posix.recvfrom uses mingw. Windows is not a POSIX OS after all
Overall, Windows itself is not compatible with the POSIX standard, but developers accustomed to the POSIX ecosystem use some wrapper libraries to wrap Windows native operations into partial POSIX APIs. Mingw is the most popular wrapper library of this kind, which is also the default libc selected by Zig on Windows.