MSVC linking woes

If you were calling “closesocket” from zig, you’d call an extern function that declares it comes from the “ws2_32” library (see ws2_32.zig). Since C doesn’t have a mechanism to tie functions to the library they come from, you have to manually specify these libraries yourself.

So in general, there are many functions provided by builtin DLL’s on Windows, like user32, kernel32, ws2_32, you learn which ones over time but when you get linker errors you can usually just try googling those functions to see if they happen to be one of these. Here’s the entry for closesocket: closesocket function (winsock.h) - Win32 apps | Microsoft Learn
You can see at the bottom it comes from ws2_32 so in your build.zig you’ll want to add a call to linkSystemLibrary("ws2_32") if your target is windows.

P.S. there is mechanism C code can use to add libraries to be linked via pragma comment lib…so sometimes libraries use this to encode library dependencies in their source code. However I don’t think every toolchain supports these, Zig being one of that that doesn’t support it.

2 Likes