#pragma comment(lib,"ws2_32.lib")报错解决办法

发布时间 2023-08-23 16:02:56作者: jsqdsg

起因

最近在van套接字(Socket)结果写C了代码后发现编译不了

编译报错

c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x4a): undefined reference to `__imp_WSAStartup'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xa7): undefined reference to `__imp_socket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xfc): undefined reference to `__imp_closesocket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x105): undefined reference to `__imp_WSACleanup'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x11d): undefined reference to `__imp_gethostname'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x155): undefined reference to `__imp_WSAGetLastError'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x181): undefined reference to `__imp_closesocket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x18a): undefined reference to `__imp_WSACleanup'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x19d): undefined reference to `__imp_gethostbyname'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x1e1): undefined reference to `__imp_WSAGetLastError'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x20d): undefined reference to `__imp_closesocket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x216): undefined reference to `__imp_WSACleanup'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x22d): undefined reference to `__imp_htons'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x26a): undefined reference to `__imp_bind'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x2a2): undefined reference to `__imp_WSAGetLastError'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x2ce): undefined reference to `__imp_closesocket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x2d7): undefined reference to `__imp_WSACleanup'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x345): undefined reference to `__imp_WSAIoctl'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x37d): undefined reference to `__imp_WSAGetLastError'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x3a9): undefined reference to `__imp_closesocket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x3b2): undefined reference to `__imp_WSACleanup'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x41d): undefined reference to `__imp_recv'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x569): undefined reference to `__imp_ntohs'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x5ad): undefined reference to `__imp_ntohs'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x724): undefined reference to `__imp_ntohs'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x77b): undefined reference to `__imp_inet_ntoa'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x7be): undefined reference to `__imp_inet_ntoa'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x962): undefined reference to `__imp_ntohs'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0x9a9): undefined reference to `__imp_ntohs'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xb2f): undefined reference to `__imp_ntohs'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xb77): undefined reference to `__imp_inet_ntoa'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xbbd): undefined reference to `__imp_inet_ntoa'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xbf5): undefined reference to `__imp_closesocket'
c:\mingwgcc9.3.0\mingw\x86_64-w64-mingw32\bin\ld.exe	C:\Users\IDC\AppData\Local\Temp\ccxKBWPc.o:IP监控.cpp:(.text+0xbfe): undefined reference to `__imp_WSACleanup'
D:\collect2.exe	[Error] ld returned 1 exit status

解决办法

再编译命令里加入

-lwsock32

就行了