【问题标题】:C undefined reference to InetPtonWC 未定义对 InetPtonW 的引用
【发布时间】:2018-08-12 06:51:54
【问题描述】:

我正在尝试使用InetPtonW

if(InetPtonW(AF_INET, argv[1], &ThisSenderInfo.sin_addr)<=0) { 
    return 1; 
}

但是我在编译时收到以下消息:

warning: implicit declaration of function 'InetPtonW' [-Wimplicit-function-declaration]
undefined reference to `InetPtonW'
collect2.exe: error: ld returned 1 exit status

我已经阅读了位于here 的文档,并且我已经遵循了所有内容,但仍然无法使其正常工作。

• 我正在使用 MinGW 编译 Ws2_32 库 gcc test.c -o test -lws2_32

• 我已经包含了所需的头文件#include &lt;ws2tcpip.h&gt;#include &lt;windows.h&gt;

• 我尝试过使用InetPton,但它返回相同的错误

• 在 Windows 10 上运行

【问题讨论】:

标签: c windows reference undefined


【解决方案1】:

我记得几个月前遇到过这个确切的问题。 @alk 的评论指出了一个问题,其 accepted answer 感觉 非常相似 与为我解决的问题。

您应该能够在#include 行之前使用#define 一个版本宏(或两个)来修复它。

虽然我强烈认为the aforementioned answer 是正确的,但我会在今天晚些时候验证时更新这个答案。

更新!

我上面引用的代码中不再包含InetPtonW,但其中包含必要的#defines。这是一个在我的机器上编译的简短示例(win10/mingw64/gcc 8.2.0):

Z:\Some\Directory&gt;gcc test.c -o test -lmswsock -lws2_32

#define NTDDI_VERSION NTDDI_VISTA
#define WINVER _WIN32_WINNT_VISTA
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>

/* This is "test.c", please pardon the lack of error checking. */

int main(void) {
    BYTE ipbuf[4] = {0};

    WSADATA wsa;
    WSAStartup(MAKEWORD(2,2), &wsa);

    printf("%d: ", InetPtonW(AF_INET, L"127.0.0.1", &ipbuf));
    for(int i = 0; i < 4; ++i)
        printf("%hhu.", ipbuf[i]);

    WSACleanup();
}

输出应如下所示:

Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32

Z:\Some\Directory>test
1: 127.0.0.1.
Z:\Some\Directory>

【讨论】:

    【解决方案2】:

    这是一个链接错误。这就是说,包含库路径,找不到给定的函数。请确保您的 InetPtonW 的 dll 库路径或确保该路径在您的系统中可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2011-05-07
      相关资源
      最近更新 更多