Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WSAStartup link error

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?

like image 803
erjik Avatar asked Jul 29 '10 03:07

erjik


People also ask

When to call WSAStartup?

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.

What is WSAStartup?

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.

What is an undefined reference unresolved external symbol error and how do I fix it?

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.

What is #pragma comment Lib Ws2_32 lib?

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.


4 Answers

#pragma comment(lib,"WS2_32") after all #include's

like image 187
Mickey Tin Avatar answered Oct 06 '22 10:10

Mickey Tin


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++.

like image 23
Tyler McHenry Avatar answered Oct 06 '22 09:10

Tyler McHenry


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.

like image 42
Hariom saxena Avatar answered Oct 06 '22 10:10

Hariom saxena


In my case, that problem was solved by adding #pragma comment(lib, "ws2_32.lib")

like image 31
Zarina Abdibaitova Avatar answered Oct 06 '22 09:10

Zarina Abdibaitova