MinGW 链接器错误: winsock

59

我正在使用MinGW编译器在Windows上编译我的带有套接字的C++应用程序。我的链接命令看起来像这样:

g++.exe -Wall -Wno-long-long -pedantic -lwsock32 -o dist/Windows/piskvorky { there are a lot of object files }

我也尝试过

g++.exe -Wall -Wno-long-long -pedantic -lws2_32 -o dist/Windows/piskvorky { there are a lot of object files }

但是在这两种情况下,我都收到了这个错误:

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'

你有任何想法可以指出问题吗?


井字游戏使用套接字? :D - Matouš Vrba
@Matous 是的,这个游戏有网络模式。 - Gaim
4个回答

145

-lws2_32 放在目标文件列表之后 - GCC 会按照它们在命令行中出现的顺序搜索库和目标文件。

只是为了帮助其他观众:

gcc hello.c -o hello.o -lws2_32

10

使用MinGW和eclipse:

菜单 >> 项目 >> 属性 >> C/C++构建 >> 设置:注册“工具设置” - MinGW C链接器 - 其他杂项:下半部分“其他对象”

添加:“D:\Programmierung\mingw\lib\libwsock32.a”例如。在任何其他属性中不需要libwsock32.a的其他条目,特别是不要在库条目中添加。也不需要与此库相关的标志。


5
答案是用英文表达的......路径不是,但这不应该是问题。 - jazzpi

1

在:

菜单 - 项目 - 属性 - c/c++构建 - 设置:注册“工具设置” - MinGW C++ 链接器 - 其他选项:底部的“其他对象”

添加:libwsock32.a 来自 MinGW 的 bin 文件夹。


-3

你好,我刚刚看了上面的问题...

首先,这里有一些观察结果...

我知道你在提到MinGw,但是你可能需要cygwin来解决这个问题,我不太了解mingw,但我更熟悉cygwin。但我知道它们是彼此的近亲。

Cygwin附带了预编译的boost库,但是谁知道它们是哪个版本。我相信可以检查,但是谁有时间去做呢?我没有链接cygwin boost库或mingw boost库,我使用gcc在Windows(cygwin)上从头构建了boost。编译很顺利。

在撰写本文时,boost的版本为1.47.0。

已经可以确定的是,cygwin使用版本(x?),而boost是1.47.0,这可能是一个重大问题。确保你知道你正在使用的boost版本。

我正在使用基于boost 1.42开发的代码,并遇到了相关的链接器错误。代码编译、头文件找到等等,但是然后我得到了未定义的引用WSA等等...opensocket这个,closesocket那个等等...

显然,为了进行网络套接字操作,boost需要一个平台库,其中在Windows中以ws2_32的形式出现,在Linux中则是socket。

因此,如果您正确使用boost并包含正确的boost系统库,则可能还需要一个特定于操作系统的库来访问某些资源(在这种情况下是网络)。

之后链接器错误就会消失。对于boost老手来说,这里正在发生什么可能很明显,但我无法通过谷歌找到清晰的答案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接