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?开发者_运维知识库
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
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.
You need to load the file using LoadLibrary if you do not have the lib file.
精彩评论