直接编译
gcc C:\test.c -o C:\test.exe
报错
C:\test.c:76:3: warning: implicit declaration of function 'inet_pton' [-Wimplicit-fun
aration]
inet_pton(AF_INET, "127.0.0.1", &(ipaddr.sin_addr));
^~~~~~~~~
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x56): undefined reference to `accept@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0xf9): undefined reference to `send@16'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x107): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x143): undefined reference to `socuser0t@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x17a): undefined reference to `socuser0t@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x1b6): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x20f): undefined reference to `htons@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x234): undefined reference to `inet_pton'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x25a): undefined reference to `connect@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x283): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x29b): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x305): undefined reference to `htons@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x32a): undefined reference to `inet_pton'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x350): undefined reference to `connect@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x379): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x391): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x422): undefined reference to `recv@16'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x441): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x457): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x4c2): undefined reference to `send@16'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x50f): undefined reference to `recv@16'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x52e): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x544): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x5af): undefined reference to `send@16'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x673): undefined reference to `WSAStartup@8'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x86a): undefined reference to `socuser0t@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x901): undefined reference to `inet_pton'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x911): undefined reference to `htons@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x93c): undefined reference to `bind@12'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x979): undefined reference to `closesocuser0t@4'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x99d): undefined reference to `listen@8'
C:\Users\user0\AppData\Local\Temp\ccARteWg.o:test.c:(.text+0x9da): undefined reference to `closesocuser0t@4'
collect2.exe: error: ld returned 1 exit status
按照网上的说话,添加 `-lws2_32`或者 `-lwsock32`
gcc C:\test.c -o C:\test.exe -lws2_32
或者
gcc C:\test.c -o C:\test.exe -lwsock32
connect@12',recv@16,send@16等函数不报错了
但是`inet_pton`函数依然报错
gcc C:\test.c -o C:\test.exe -lws2_32
如下:
C:\test.c:76:3: warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-decl
aration]
inet_pton(AF_INET, "127.0.0.1", &(ipaddr.sin_addr));
^~~~~~~~~
C:\Users\user0\AppData\Local\Temp\ccz8vEmu.o:test.c:(.text+0x234): undefined reference to `inet_pton'
C:\Users\user0\AppData\Local\Temp\ccz8vEmu.o:test.c:(.text+0x32a): undefined reference to `inet_pton'
C:\Users\user0\AppData\Local\Temp\ccz8vEmu.o:test.c:(.text+0x901): undefined reference to `inet_pton'
collect2.exe: error: ld returned 1 exit status
这个提示就是inet_pton函数没有定义的意思
奇怪的是VS下是能正常编译的,但是mingw下的gcc不能正常编译
根据网上的说法
该报错是因为
gcc test.c -o inet_pton -lws2_32
D:\temp\SysTmp\ccH2BQCx.o:inet_pton.c:(.text+0x29): undefined reference to `inet_pton'
collect2.exe: error: ld returned 1 exit status
错误原因:Windows gcc 默认的 _WIN32_WINNT 是 502 Windows Server 2003
#include "stdio.h"
#include "windows.h"
#include "ws2tcpip.h"
#include <WinSock2.h>
void main(void)
{
......
struct sockaddr_in ipaddr;
inet_pton(AF_INET, "127.0.0.1", &(ipaddr.sin_addr));
......
}
gcc test.c -o test.exe -lws2_32
错误
改为
//----------------------------------
#include "stdio.h"
#define _WIN32_WINNT 0x0600 //这个是需要添加的
#include "windows.h"
#include "ws2tcpip.h"
#include <WinSock2.h>
void main(void)
{
......
struct sockaddr_in ipaddr;
inet_pton(AF_INET, "127.0.0.1", &(ipaddr.sin_addr));
......
}
gcc test.c -o test.exe -lws2_32
就能正常编译
网上解释,因为需要这些头文件,但是Windows gcc 默认的 _WIN32_WINNT 是 502 Windows Server 2003,使用找不到头文件
#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 <--win gcc环境默认的版本
#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
但是实际操作下来,发现行个p
这行也跟着报错
#define _WIN32_WINNT 0x0600
但是很奇怪,为什么VS编译就不报错,而gcc下编译就要报错呢?
于是在VS中把inet_pton()函数定义的代码扒了出来,略微修改了一下,就能用了 inet_pton()函数代码
void inet_pton(int af, const char *src, void *dst){
struct sockaddr_in ip4;
struct sockaddr_in6 ip6;
char ipaddr[64];
sprintf(ipaddr,"%s:2020",src);
int addr_size=-1;
if (af == AF_INET){
addr_size= sizeof(SOCKADDR_IN);;
WSAStringToAddress( (LPSTR)ipaddr,af, NULL, (LPSOCKADDR)&ip4,&addr_size );
memcpy(dst,&(ip4.sin_addr),4);
} else if (af==AF_INET6){
addr_size=sizeof(SOCKADDR_IN6);
WSAStringToAddress( (LPSTR)ipaddr,af, NULL, (LPSOCKADDR)&ip6,&addr_size );
memcpy(dst,&(ip6.sin6_addr),16);
}
//printf("ipaddr len=%d\nAF_INET6=%d,AF_INET=%d,af=%d\n",addr_size,AF_INET6,AF_INET,af);
}
把这个函数加到程序int main()之前就能用了
#include "stdio.h"
#include "windows.h"
#include "ws2tcpip.h"
#include <WinSock2.h>
void inet_pton(int af, const char *src, void *dst){
struct sockaddr_in ip4;
struct sockaddr_in6 ip6;
char ipaddr[64];
sprintf(ipaddr,"%s:2020",src);
int addr_size=-1;
if (af == AF_INET){
addr_size= sizeof(SOCKADDR_IN);;
WSAStringToAddress( (LPSTR)ipaddr,af, NULL, (LPSOCKADDR)&ip4,&addr_size );
memcpy(dst,&(ip4.sin_addr),4);
} else if (af==AF_INET6){
addr_size=sizeof(SOCKADDR_IN6);
WSAStringToAddress( (LPSTR)ipaddr,af, NULL, (LPSOCKADDR)&ip6,&addr_size );
memcpy(dst,&(ip6.sin6_addr),16);
}
//printf("ipaddr len=%d\nAF_INET6=%d,AF_INET=%d,af=%d\n",addr_size,AF_INET6,AF_INET,af);
}
void main(void)
{
......
struct sockaddr_in ipaddr;
inet_pton(AF_INET, "127.0.0.1", &(ipaddr.sin_addr));
......
}
gcc test.c -o test.exe -lws2_32
正常编译
不知道为什么,Windows下gcc找不到inet_pton()函数的定义。
只好把VS中的inet_pton()函数定义移过来补上。
由于CSDN把我的文章强行设置为VIP,还不能取消,故设置此网站
遵循CC 4.0 BY-SA版权协议