C++ 对“__imp_WSACleanup”的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18559028/
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
undefined reference to `__imp_WSACleanup'
提问by user2682541
This is my first program with winsock. As you can see, I've #include <winsock2.h>
and linked ws2_32.dll
, but the code still doesn't compile:
这是我第一个使用 winsock 的程序。如您所见,我已经#include <winsock2.h>
链接了ws2_32.dll
,但代码仍然无法编译:
#include<winsock2.h>
#pragma comment(lib, "ws2_32")
class CInitSock{
public:
CInitSock(BYTE minorVer=2,BYTE majorVer=2){
//initialize WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD(minorVer,majorVer);
if(::WSAStartup(sockVersion,&wsaData)!=0){
exit(0);
}
}
//release winSock libary
~CInitSock(){
::WSACleanup();
}
};
#include "CInitSock.h"
#include<stdio.h>
CInitSock initSock;
int main(void){
char szHost[256];
::gethostname(szHost,256);
hostent *phost = ::gethostbyname(szHost);
in_addr addr;
for(int i = 0;;i++){
char *p = phost->h_addr_list[i];
if(p==NULL){
break;
}
memcpy(&addr.S_un.S_addr,p,phost->h_length);
char *szIp = ::inet_ntoa(addr);
printf("%s \n",szIp);
}
}
This is the error:
这是错误:
mingw32-make.exe -f "D:\project\c_program\Makefile.win" all
g++.exe GetAllIPs.o -o win_socket.exe -L"D:/tools/develepment/Dev-Cpp/MinGW64/x86_64- w64-mingw32/lib" -L"D:/tools/develepment/Dev-Cpp/MinGW64/lib32" -static-libgcc -mwindows -g3
GetAllIPs.o: In function `main':
D:\project\c_program/GetAllIPs.cpp:6: undefined reference to `__imp_gethostname'
D:\project\c_program/GetAllIPs.cpp:7: undefined reference to `__imp_gethostbyname'
D:\project\c_program/GetAllIPs.cpp:15: undefined reference to `__imp_inet_ntoa'
GetAllIPs.o: In function `CInitSock::CInitSock(unsigned char, unsigned char)':
D:\project\c_program/CInitSock.h:10: undefined reference to `__imp_WSAStartup'
GetAllIPs.o: In function `CInitSock::~CInitSock()':
D:\project\c_program/CInitSock.h:16: undefined reference to `__imp_WSACleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe: *** [win_socket.exe] Error 1
Now I'm totally confused...
现在我完全糊涂了......
回答by Martin Schlott
The pragma you use only works for the Visual C++ Compiler and will be ignored by the gcc
您使用的编译指示仅适用于 Visual C++ 编译器,将被 gcc 忽略
#pragma comment(lib, "ws2_32")
you have to add the ws2_32.lib it manually in the makefile. like:
您必须在 makefile 中手动添加 ws2_32.lib。喜欢:
-L"ws2_32"
(I guess it was without the ".lib" at the end)
(我猜最后没有“.lib”)
at the end of the g++ line. You have of course add the full path which I have not by hand at the moment.
在 g++ 行的末尾。您当然已经添加了我目前没有手动添加的完整路径。
回答by L.ling
I met the same problem with you. I solved it by adding a command -lwsock32
.
you can add the command according follow steps:
我和你遇到了同样的问题。我通过添加命令解决了它-lwsock32
。您可以按照以下步骤添加命令:
- tools
- compiler options
- choose
general
- click
add the following commands when calling the compilers
- 工具
- 编译器选项
- 选择
general
- 点击
add the following commands when calling the compilers
then you can add the above command -lwsock32
.
然后你可以添加上面的命令-lwsock32
。
回答by Walterti
In DevC++, navigate to Project >> Project Options(or via usually ctrl+h); then in the "Parameters" tab there is a button "Add Library or Object" and then add libws2_32.a
.
在 DevC++ 中,导航到Project >> Project Options(或通常通过ctrl+ h);然后在“参数”选项卡中有一个按钮“添加库或对象”,然后添加libws2_32.a
。
回答by Beyondo
add
添加
-lwsock32
to your command-lineinstead of #pragma
when compiling with MinGW
到您的命令行,而不是#pragma
在使用MinGW编译时
g++ src/main.cpp -o release/myApp.exe -lwsock32