【问题标题】:Implicit declaration of function 'getaddrinfo' on MinGWMinGW 上函数“getaddrinfo”的隐式声明
【发布时间】:2012-09-27 18:14:57
【问题描述】:

我有一个使用getaddrinfo() 的C 程序。它可以在 Linux 和 Mac OS X 上正常运行。

我正在将其移植到 Windows。

当我编译它(使用 MinGW gcc)时,我收到以下警告:

ext/socket/socket.c: In function 'sl_tcp_socket_init':
ext/socket/socket.c:98:5: warning implicit declaration of function 'getaddrinfo' [-Wimplicit-function-declaration]
ext/socket/socket.c:104:9: warning implicit declaration of function 'freeaddrinfo' [-Wimplicit-function-declaration]

然后整个事情无法链接到对getaddrinfo()freeaddrinfo() 的未定义引用。

现在,根据this MSDN pagegetaddrinfo()在Windows上是支持的,位于头文件Ws2tcpip.h和库文件Ws2_32.lib中。

我包含Ws2tcpip.h 并与-lWs2_32 链接,所以我不确定为什么这不起作用。

【问题讨论】:

    标签: c winapi mingw winsock2 getaddrinfo


    【解决方案1】:

    如果您查看 ws2tcpip.h 的第 297 行,您可以看到检查了 _WIN32_WINNT 的值。

    #if (_WIN32_WINNT >= 0x0501)
    void WSAAPI freeaddrinfo (struct addrinfo*);
    int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
                    struct addrinfo**);
    int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
                   char*,DWORD,int);
    #else
    /* FIXME: Need WS protocol-independent API helpers.  */
    #endif
    

    在包含之前只需#define _WIN32_WINNT

    【讨论】:

      【解决方案2】:

      如果您想让您的代码在编译器范围内使用,您实际上还应该使用与_WIN32_WINNT 相同的操作系统版本来定义NTDDI_VERSION。如果没有这个定义,只有 _WIN32_WINNT 不会让您将 getaddrinfo() 与某些编译器(即 Watcom)一起使用。最好以与 Windows SDK 相同的方式包装它:

      #define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)    ver##0000
      #define _NTDDI_VERSION_FROM_WIN32_WINNT(ver)     _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)
      
      #ifndef _WIN32_WINNT
      #  define _WIN32_WINNT 0x501
      #endif
      #ifndef NTDDI_VERSION
      #  define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)
      #endif
      

      【讨论】:

        【解决方案3】:

        据说解决这个问题的正确方法是:

        #define WINVER WindowsXP
        

        或者更明智的做法是将-DWINVER=WindowsXP 添加到您的CPPFLAGS

        参考:http://mingw.5.n7.nabble.com/Undefined-reference-to-getaddrinfo-td5694.html

        注意:但对我不起作用。

        【讨论】:

          猜你喜欢
          • 2021-01-20
          • 1970-01-01
          • 2017-06-04
          • 2014-06-28
          • 2022-01-16
          • 2011-10-05
          • 2013-10-28
          • 2019-07-16
          相关资源
          最近更新 更多