6

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?

MD XF
  • 7,860
  • 7
  • 40
  • 71
maximus
  • 4,201
  • 15
  • 64
  • 117

4 Answers4

17

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

Mickey Tin
  • 3,408
  • 10
  • 42
  • 71
  • 1
    This worked for me on Visual Studio. Basically we need to provide the symbols for the functions to the linker while linking. – prashanthns Mar 15 '17 at 09:18
9

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

Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
  • http://msdn.microsoft.com/en-us/library/ms911778.aspx It is written there about how the WSAStartup works. It uses ws2.dll. But nothing is written about ws2_32.dll – maximus Jul 29 '10 at 04:56
1

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.

1

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

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Zrn-dev
  • 99
  • 5