sometimes I get this linker error
LNK2019: unresolved external symbol _WSACleanup@0 referenced in function _main
and sometimes it works OK. Solution is usually to place pragma directive below include directives

#pragma comment(lib, "wsock32.lib")

First, can someone explain in simple words what this means?
Maybe it's some kind of bug, it seems that problem sometimes rise only in MSVC++.
I use MSVC++ .net v2002

Thank you

Well the linking error means it finds the prototype of the function but doesn't find the actual defination of the function. I am not sure why it does that since I would not be an VC++ expert. Maybe look up how the pragma comment has to be.

If you think that it is a bug in Visual Studio, it is not. Refer the following URL.
#pragma comment

You need to link the wsock32.lib or ws2_32.lib ( Winsock Library ) to use the APIs that are needed for WinSock programming in Windows. Visual Studio by default links the kernel32.lib user32.lib gdi32.lib libraries when you build a project. If you are using other libraries ( e.g. ws2_32.lib, ComCtl32.lib ), you will have to explicitly tell that to the linker.

One way is to use the method you have already used. Other methods are setting the "Additional Libraries Used" field in Project Properties->Linker->Input.

Also you can select add Existing Item to Project and browse for the LIbraries you are using and select them. But this method is not encouraged because this uses Absolute Paths and if you compile it in another machine where the Library Path is different, the linker will fail.

This article has been dead for over six months. Start a new discussion instead.