socklen_t undeclared when compiling .c code

后端 未结 5 904
-上瘾入骨i
-上瘾入骨i 2020-12-29 07:43

I am trying to compile this .c code in windows using MinGW (gcc file.c -o compiled.exe):

/***************************************************/ 
/* AUTHOR             


        
相关标签:
5条回答
  • 2020-12-29 07:48

    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>   
    
    0 讨论(0)
  • 2020-12-29 07:49

    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.

    0 讨论(0)
  • 2020-12-29 07:49

    Under mingw you can try to include ws2tcpip.h

    #include <ws2tcpip.h>
    
    0 讨论(0)
  • 2020-12-29 07:53

    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.

    0 讨论(0)
  • 2020-12-29 07:59

    According to the Unix Specification, socket.h makes available a type, socklen_t, which is an unsigned opaque integral type of length of at least 32 bits. Apparently MingW doesn't include it.

    You can define it as:

    #include <stdint.h>
    typedef uint32_t socklen_t;
    
    0 讨论(0)
提交回复
热议问题