gpt4 book ai didi

C 未定义对 InetPtonW 的引用

转载 作者:可可西里 更新时间:2023-11-01 13:27:30 25 4
gpt4 key购买 nike

我正在尝试使用 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 的文档我已经按照所有内容进行操作,但仍然无法正常工作。

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

• 我已经包含了所需的头文件 #include <ws2tcpip.h>#include <windows.h>

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

• 在 Windows 10 上运行

最佳答案

我记得几个月前遇到过这个确切的问题。 @alk 的评论指向一个问题 accepted answer感觉为我修复它的方法非常相似。

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

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

更新!

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

Z:\Some\Directory>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>

关于C 未定义对 InetPtonW 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51806450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com