C++ MinGW 链接器错误:winsock
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2033608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MinGW linker error: winsock
提问by Gaim
I am using MinGW compiler on Windows to compile my C++ application with sockets. My command for linking looks like:
我在 Windows 上使用 MinGW 编译器来编译我的带有套接字的 C++ 应用程序。我的链接命令如下所示:
g++.exe -Wall -Wno-long-long -pedantic -lwsock32 -o dist/Windows/piskvorky { there are a lot of object files }
and I have also tried
我也试过
g++.exe -Wall -Wno-long-long -pedantic -lws2_32 -o dist/Windows/piskvorky { there are a lot of object files }
but in both case I get this error:
但在这两种情况下,我都会收到此错误:
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'
Do you have any ideas where the problem can be, please?
你有什么想法可能出问题吗?
回答by
Put the -lws2_32
AFTER the list of object files - GCC searches libraries and object files in the order they appear on the command line.
将-lws2_32
AFTER放在目标文件列表之后 - GCC 按照它们在命令行中出现的顺序搜索库和目标文件。
Just to help the other viewers out there:
只是为了帮助那里的其他观众:
gcc hello.c -o hello.o -lws2_32
回答by Kai
with mingw on eclipse: Menu - Projekt - Properties - c/c++Build - Settings: Register "Tool Settings" - MinGW C Linker - Miscellaneous: Lower Part "other objects" add: "D:\Programmierung\mingw\lib\libwsock32.a" for example. No other entries for libwsock32.a on any other properties required, especially not in Library-entries. Also no flags relating to this Lib.
在 Eclipse 上使用 mingw:菜单 - Projekt - 属性 - c/c++Build - 设置:注册“工具设置” - MinGW C 链接器 - 杂项:下部“其他对象”添加:“D:\Programmierung\mingw\lib\例如 libwsock32.a”。libwsock32.a 不需要任何其他属性的其他条目,尤其是在库条目中。也没有与此 Lib 相关的标志。
回答by Anand Paul
In:
在:
Menu - Project - Properties - c/c++Build - Settings: Register "Tool Settings" - MinGW C++ Linker - Miscellaneous: Lower Part "other objects"
菜单-项目-属性-c/c++Build-设置:注册“工具设置”-MinGW C++链接器-杂项:下部分“其他对象”
Add: libwsock32.afrom bin folder of MinGW.
添加:libwsock32.a来自MinGW 的bin 文件夹。
回答by Joe R.
Greetings I just went through the problem above....
问候我刚刚解决了上面的问题....
So here are some observations first off....
所以这里首先有一些观察......
I know you are refering to MinGw, but you may need cygwin to get around this, I don't know mingw that well, I know cygwin better. But I know that they are cousins of each other.
我知道你指的是 MinGw,但你可能需要 cygwin 来解决这个问题,我不太了解 mingw,我更了解 cygwin。但我知道他们是彼此的表亲。
Cygwin comes with the precompiled boost libraries, but who knows of which version they are. I'm sure it's possible to check, but who has time to do that right? I do not link against cygwin boost libraries, or the mingw boost libraries, I built boost from scratch using gcc on windows (cygwin). The compile did fine.
Cygwin 带有预编译的 boost 库,但谁知道它们是哪个版本。我确定可以检查,但谁有时间这样做呢?我不链接 cygwin boost 库或 mingw boost 库,我在 windows (cygwin) 上使用 gcc 从头开始构建 boost。编译做得很好。
At the time of this writing boost is on version 1.47.0 I beleive.
在撰写本文时,我相信 boost 版本为 1.47.0。
Already that fact that cygwin uses version (x?) and boost is 1.47.0 could be a major issue. Make you know what you are using in the way of boost.
cygwin 使用版本 (x?) 并且 boost 是 1.47.0 的事实可能是一个主要问题。让你知道你在使用boost的方式。
I was using code developed on boost 1.42, and had related linker errors. The code compiled, headers were found, etc..etc... but then I got the undefined reference to WSA etc...etc...opensocket this, close socket that, etc....
我使用的是在 boost 1.42 上开发的代码,并且有相关的链接器错误。编译的代码,找到了头文件,等等......但后来我得到了对 WSA 等的未定义引用......等等......打开套接字这个,关闭套接字等等......
So, apparantly boost, in order to do network sockets, requires a platform library, in which was found in the form of ws2_32 for windows, and socket for linux for sure.
因此,显然提升,为了进行网络套接字,需要一个平台库,其中在 windows 中以 ws2_32 的形式找到,当然在 linux 中以 socket 的形式找到。
So if you are properly using boost, and including the correct boost system libraries, you may also need an OS specific library to access some resources (networking in this case).
因此,如果您正确使用 boost,并包含正确的 boost 系统库,您可能还需要一个特定于操作系统的库来访问某些资源(在这种情况下是网络)。
Linker errors dissappear after that. It's probably obvious to boost veterans what's going on here, but I was unable to find a clear answer via google.
之后链接器错误消失。促进退伍军人这里发生的事情可能很明显,但我无法通过谷歌找到明确的答案。