I am using EVC++ and I want to compile the program which uses the sockets. I've included
#include <winsock2.h>
And I have included in project properties a path to Ws2.lib But still get the error at link step:
error LNK2019: unresolved external symbol WSAStartup referenced in function ...
How to resolve this problem?
An application must call the WSAStartup function to initialize Winsock, regardless of which version of Winsock is being used. WSAStartup initialized Winsock2. dll and a WSADATA structure that contains the details of the Winsock implementation. When an application or DLL has finished using Winsock2.
The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation.
So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.
The #pragma comment indicates to the linker that the Ws2_32. lib file is needed. Begin programming the Winsock application. Use the Winsock API by including the Winsock 2 header files.
#pragma comment(lib,"WS2_32")
after all #include
's
You haven't linked your program with the winsock library. The Winsock 2 library is called ws2_32.lib (static) or ws2_32.dll (dynamic). It should already be on your system; you just need to tell your compiler/linker to link your program against it. The method of doing this varies by compiler, and unfortunately I'm not familiar with EVC++.
Have seen this error in codeblock IDE using MinGW. Tried many ways but finally found this solution.
Add(your the path for MinGW installed in your system) C:\Program Files (x86)\CodeBlocks\MinGW\lib\libws2_32 C:\Program Files (x86)\CodeBlocks\MinGW\lib\libwsock32 in codeblock IDE.
How to add: Go to project. build options. Linker setting. click add of link library.
And its done.
In my case, that problem was solved by adding
#pragma comment(lib, "ws2_32.lib")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With