- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 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/
我正在尝试使用 InetPtonW : if(InetPtonW(AF_INET, argv[1], &ThisSenderInfo.sin_addr)和 #include • 我试过使用InetP
我刚刚开始学习C,现在正在学习如何使用winsock2-Header。要将 IP 地址从字符串表示形式转换为二进制形式,我使用函数 InetPtonW。我的问题是这个函数总是返回 IP 地址 1.0.
我是一名优秀的程序员,十分优秀!