undefined reference to `inet_pton' under MSYS

时间:2020-09-10 03:18:21 来源:

【摘要】 undefined reference to `inet_pton' under MSYS考必过小编为大家整理了关于undefined reference to `inet_pton' under MSYS的信息,希望可以帮助到大家!

undefined reference to `inet_pton' under MSYS

标签:win7原因tcpipstatuscodeufsinet_addrstrlenwindows 7

#include "stdio.h"
#include "stdint.h"
#include "windows.h"
#include "ws2tcpip.h"

void main(void)
{
  struct sockaddr_in sa;
  char str[INET_ADDRSTRLEN];
  inet_pton(AF_INET, "192.0.2.33", (char *)(&(sa.sin_addr)));
  inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
  printf("%s\n", str); // prints "192.0.2.33"
}

gcc -w inet_pton.c -lws2_32 -o inet_pton
D:\zDown\temp\SysTmp\ccH2BQCx.o:inet_pton.c:(.text+0x29): undefined reference to `inet_pton‘
D:\zDown\temp\SysTmp\ccH2BQCx.o:inet_pton.c:(.text+0x50): undefined reference to `inet_ntop‘
collect2.exe: error: ld returned 1 exit status

错误原因:MSYS 默认的 _WIN32_WINNT 是 502Windows Server 2003

#include "stdio.h"
#include "stdint.h"
#define _WIN32_WINNT 0x0600
#include "windows.h"
#include "ws2tcpip.h"

void main(void)
{
  struct sockaddr_in sa;
  char str[INET_ADDRSTRLEN];
  inet_pton(AF_INET, "192.0.2.33", (char *)(&(sa.sin_addr)));
  inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
  printf("%s\n", str); // prints "192.0.2.33"
}

gcc -w inet_pton.c -lws2_32 -o inet_pton

编译成功

分析原因

#define _WIN32_WINNT_NT4                    0x0400 // Windows NT 4.0
#define _WIN32_WINNT_WIN2K                  0x0500 // Windows 2000
#define _WIN32_WINNT_WINXP                  0x0501 // Windows XP
#define _WIN32_WINNT_WS03                   0x0502 // Windows Server 2003    <--- MSYS 环境默认的版本
#define _WIN32_WINNT_WIN6                   0x0600 // Windows Vista
#define _WIN32_WINNT_VISTA                  0x0600 // Windows Vista
#define _WIN32_WINNT_WS08                   0x0600 // Windows Server 2008
#define _WIN32_WINNT_LONGHORN               0x0600 // Windows Vista
#define _WIN32_WINNT_WIN7                   0x0601 // Windows 7
#define _WIN32_WINNT_WIN8                   0x0602 // Windows 8
#define _WIN32_WINNT_WINBLUE                0x0603 // Windows 8.1
#define _WIN32_WINNT_WINTHRESHOLD           0x0A00 // Windows 10
#define _WIN32_WINNT_WIN10                  0x0A00 // Windows 10

mingw/i686-w64-mingw32/include/ws2tcpip.h
#if (_WIN32_WINNT >= 0x0600)
...
#define InetNtopA inet_ntop

WINSOCK_API_LINKAGE LPCWSTR WSAAPI InetNtopW(INT Family, PVOID pAddr, LPWSTR pStringBuf, size_t StringBufSIze);
WINSOCK_API_LINKAGE LPCSTR WSAAPI InetNtopA(INT Family, PVOID pAddr, LPSTR pStringBuf, size_t StringBufSize);

#define InetNtop __MINGW_NAME_AW(InetNtop)

#define InetPtonA inet_pton

WINSOCK_API_LINKAGE INT WSAAPI InetPtonW(INT Family, LPCWSTR pStringBuf, PVOID pAddr);
WINSOCK_API_LINKAGE INT WSAAPI InetPtonA(INT Family, LPCSTR pStringBuf, PVOID pAddr);

#define InetPton __MINGW_NAME_AW(InetPton)

#endif /*(_WIN32_WINNT >= 0x0600)*/

undefined reference to `inet_pton' under MSYS

标签:win7原因tcpipstatuscodeufsinet_addrstrlenwindows 7

以上就是undefined reference to `inet_pton&#39; under MSYS的内容,更多资讯请及时关注考必过网站,最新消息小编会第一时间发布,大家考试加油!

上一篇      下一篇
前端相关推荐 更多>>
JavaScript常见问题:TypeError: ‘undefined' is not an object JavaScript常见问题:TypeError: ‘undefined' is not a function Vue常见问题解决:Cannot set property 'xxx' of undefined php--Undefined variable警告处理 JavaScript常见问题:ReferenceError: event is not defined Your project does not reference ".NETFramework,V asp.net core 3.1 自定义中间件实现jwt token认证 centos 7 Docker容器启动报WARNING: IPv4 forwarding
前端热点专题 更多>>
热点问答
国家公务员考试年龄限制是多少 公务员国考和省考考试内容有什么区别 函授大专学历能不能考公务员 国家公务员考试考点能自己选择吗 新闻学专业能报考2022年公务员考试吗 什么是联合培养研究生 什么是破格录取研究生 什么人不适合读研 研究生报名户口所在地填什么 研究生结业和毕业有什么区别
网站首页 网站地图 返回顶部
考必过移动版 https://m.kaobiguo.net