Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting linker errors for ws2_32.dll in my C program?

I am writing my program in Visual Studio 2010. I am unable to link a file named ws2_32.dll with my project.

Can anyone tell me how I can do that?

like image 804
Sanjay Avatar asked Mar 07 '11 13:03

Sanjay


2 Answers

The first order of business is importing the header file that defines functions exported by ws2_32.dll. You do that by adding the following statement to the top of any source file that you wish to call those functions in:

#include <winsock2.h>

Then, you have to tell the linker where it can find the import library for that DLL. There are two ways to do that in Visual Studio, but the simplest way is adding the following line to your source code:

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

You could also add it as a dependency to your linker's "Additional Dependencies" property (find that under your Project Properties -> Configuration Properties -> Linker -> Input).

MSDN also has a getting started guide that walks you through creating a basic Winsock application. Make sure that you've read it before proceeding any further.

like image 40
Cody Gray Avatar answered Sep 22 '22 04:09

Cody Gray


Typically you dont link to ws2_32.dll directly but to WS2_32.Lib, which you can find in the Windows SDK. So in your code you write

#include <winsock2.h>

and to your linker-settings you add WS2_32.Lib and you're good to go.

The Windows SDK is here:

http://msdn.microsoft.com/en-us/windows/bb980924.aspx

like image 104
Bernd Elkemann Avatar answered Sep 19 '22 04:09

Bernd Elkemann