socklen_t undeclared when compiling .c code socklen_t undeclared when compiling .c code windows windows

socklen_t undeclared when compiling .c code


Under mingw you can try to include ws2tcpip.h

#include <ws2tcpip.h>


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.