MinGW链接器错误:winsock

Gai*_*aim 46 c++ linker mingw winsock

我在Windows上使用MinGW编译器来编译带有套接字的C++应用程序.我的链接命令如下:

g++.exe -Wall -Wno-long-long -pedantic -lwsock32 -o dist/Windows/piskvorky { there are a lot of object files }
Run Code Online (Sandbox Code Playgroud)

而我也试过了

g++.exe -Wall -Wno-long-long -pedantic -lws2_32 -o dist/Windows/piskvorky { there are a lot of object files }
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下我都会收到此错误:

build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0x33): undefined reference to `closesocket@4'
build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0xd0): undefined reference to `send@16'
build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0x1ee): undefined reference to `recv@16'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x184): undefined reference to `WSAStartup@8'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1a5): undefined reference to `closesocket@4'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1cb): undefined reference to `closesocket@4'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1d3): undefined reference to `WSACleanup@0'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x6fe): undefined reference to `bind@12'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x724): undefined reference to `listen@8'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x7f0): undefined reference to `gethostbyaddr@12'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x83c): undefined reference to `socket@12'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x86f): undefined reference to `htons@4'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x8b5): undefined reference to `connect@12'
build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x9c6): undefined reference to `accept@12'
Run Code Online (Sandbox Code Playgroud)

你有什么想法可以解决问题吗?

小智 116

-lws2_32AFTER 放在目标文件列表中 - GCC按照它们在命令行中出现的顺序搜索库和目标文件.

只是为了帮助其他观众:

gcc hello.c -o hello.o -lws2_32
Run Code Online (Sandbox Code Playgroud)


小智 8

with mingw on eclipse:菜单 - Projekt - 属性 - c/c ++构建 - 设置:注册"工具设置" - MinGW C链接器 - 其他:下部"其他对象"添加:"D:\ Programmierung\mingw\lib\libwsock32.a"例如.libwsock32.a上没有其他任何属性需要,特别是在库条目中没有.也没有与此Lib有关的标志.

  • 答案是英文的......路径不是,但这应该不是问题. (5认同)