socklen_t undeclared when compiling .c code

Figure out which .h file it is defined in, and include it. On a Unix/Linux box, I'd start with a find/grep in /usr/include

$ find /usr/include -name \*.h -print0 |xargs -0 grep -w socklen_t
...
/usr/include/unistd.h:typedef __socklen_t socklen_t;
...
/usr/include/sys/socket.h:         socklen_t *__restrict __addr_len);

Looks like it's defined in unistd.h - but you've already got that one included, so I guess you're covered on that side. I don't know how you'd find which file to include on the Windows side.


Check your socket.h - that's most likely where it's defined. Your code compiles fine with CygWin since the socket.h contains (by virtue of the fact it includes cygwin/socket.h):

typedef int socklen_t;

As a kludge, you could try adding that line to your own code. But you should still investigate why it's missing and maybe raise a bug report.

There's a great many pages complaining that MinGW doesn't support socklen_t, for example here, here, here and here, the last of which states that it lives in ws2tcpip.h as I defined it in my kludge above.


For Windows, to get a definition for socklen_t:

#include <ws2tcpip.h>

Valid as of VS2017 and this writing; based on https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getnameinfo.

Two other headers I've found I needed to port a particular Linux based socket source file to Windows are:

#include <winsock2.h>
#include <Ws2ipdef.h>   

Under mingw you can try to include ws2tcpip.h

#include <ws2tcpip.h>