简体   繁体   English

我实际上是否必须链接Ws2_32.lib?

[英]Do I actually have to link Ws2_32.lib?

All Winsock2 examples at MSDN shows that I have to statically link Winsock2 libarary as: MSDN上的所有Winsock2示例都显示我必须将Winsock2库静态链接为:

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

Ensure that the build environment links to the Winsock Library file ?>Ws2_32.lib. 确保构建环境链接到Winsock库文件?> Ws2_32.lib。 Applications that use Winsock must be linked with the Ws2_32.lib >library file. 使用Winsock的应用程序必须与Ws2_32.lib> library文件链接。 The #pragma comment indicates to the linker that the Ws2_32.lib >file is needed. #pragma注释向链接器指示需要Ws2_32.lib> file。

But why I use it instead of simple loading an existing in Windows Ws2_32.dll (since Windows 2003 as I understand according to requirements at MSDN https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-recv ) 但是为什么我使用它而不是简单地加载Windows Ws2_32.dll中的现有文件(因为据我了解,根据MSDN https://docs.microsoft.com/zh-cn/windows/win32/api/winsock2/的要求,从Windows 2003开始nf-winsock2-recv

So I can use something like this: 所以我可以这样使用:

typedef int WSAAPI(WINAPI* recv_func)(SOCKET s, char *buf, int len, int flags);

HINSTANCE hGetProcIDDLL = LoadLibraryA("ws2_32.dll");
my_recv = (recv_func)GetProcAddress(hGetProcIDDLL, "recv");

Or I can just use winsock2.h header and compile a program with /MD flag: 或者我可以只使用winsock2.h标头并使用/ MD标志编译程序:

include <winsock2.h>
//#pragma comment(lib, "ws2_32.lib")

Is it possible? 可能吗? Can I use /MD or load ws2_32.dll dynamically as in first example without statically linking ws2_32.lib to my application because all Windows since Win2003 have ws2_32.dll it in System32 folder? 是否可以像第一个示例一样使用/ MD或动态加载ws2_32.dll,而无需将ws2_32.lib静态链接到我的应用程序,因为Win2003以来的所有Windows在System32文件夹中都包含ws2_32.dll?

The library ws2_32.lib is an import library. ws2_32.lib库是一个导入库。 It consist of small stubs that will redirect to the actual implementation in ws2_32.dll. 它由一些小的存根组成,这些存根将重定向到ws2_32.dll中的实际实现。 The DLL will be loaded at program load time. DLL将在程序加载时加载。 It's called Load-Time Dynamic Linking . 这称为“ 加载时动态链接”

You can verify this by comparing the size of both files. 您可以通过比较两个文件的大小来验证这一点。 Additional you can use dumpbin -symbols ws2_32.lib . 另外,您可以使用dumpbin -symbols ws2_32.lib It shows you none of the functions you might expect from the ws2.h prototypes. 它没有显示您可能从ws2.h原型获得的任何功能。

Well, you already load the DLL dynamically, at load time. 好了,您已经在加载时动态加载了DLL。 It would also be possible to do this at run-time with LoadLibrary and GetProcAddress . 也可以在运行时使用LoadLibraryGetProcAddress But this is cumbersome and don't give you any benefit. 但这很麻烦,不会给您带来任何好处。

Edit: You also link other import libraries without explicitly specifying it. 编辑:您还链接其他导入库,而无需显式指定它。 The project inherits from property sheets (see menu: View -> Other Windows -> Property Manager ) as well as from built-in rules. 该项目继承自属性表(请参阅menu: View -> Other Windows -> Property Manager )以及内置规则。 You can see the resulting linker command line in the projects properties. 您可以在项目属性中看到生成的链接器命令行。 Select the project and press alt-Enter to open the project properties and navigate then to Configuration Properties -> Linker -> Input to display the linker command line. 选择项目,然后按alt-Enter打开项目属性,然后导航到“ Configuration Properties -> Linker -> Input以显示链接器命令行。 With VS2017 Win32 C++ projects you find these import libraries: 在VS2017 Win32 C ++项目中,您可以找到以下导入库:

"kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" “ kernel32.lib”“ user32.lib”“ gdi32.lib”“ winspool.lib”“ comdlg32.lib”“ advapi32.lib”“ shell32.lib”“ ole32.lib”“ oleaut32.lib”“ uuid.lib” “ odbc32.lib”“ odbccp32.lib”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在Windows中静态链接ws2_32.lib吗? C ++ MFC - Can I statically link ws2_32.lib in windows? c++ MFC ws2_32.lib与libws2_32.a有什么区别,以及如何将libws2_32链接到NB项目? - ws2_32.lib vs. libws2_32.a, what's the difference and how to link libws2_32 to NB project? VS2010缺少ws2_32.lib。怎么办? - ws2_32.lib missing from VS2010.What to do? C++ 未定义对“WSAStartup@8”的引用,即使我链接了 ws2_32.lib - C++ undefined reference to 'WSAStartup@8' even though i linked ws2_32.lib Winsock2.h vs winsock2.h和wsock32.lib vs ws2_32.lib - Winsock2.h vs winsock2.h and wsock32.lib vs ws2_32.lib 为什么我必须链接到opengl32.lib - why do I have to link against opengl32.lib 如何使用Visual Studio 2017构建针对Windows XP的Winsock2(WS2_32.lib)应用程序 - How to build a Winsock2 (WS2_32.lib) application targeting Windows XP with Visual Studio 2017 如何使Ws2_32.lib与针对Windows 8.1的VS 2013 Project一起使用 - How to make Ws2_32.lib work with VS 2013 Project targeted at Windows 8.1 为什么在安装VSTS 2010 SP1后找不到Ws2_32.lib - why cannot find Ws2_32.lib after installing VSTS 2010 SP1 当我有 glew32 时,为什么我需要链接到 opengl32? - Why do I need to link against opengl32 when I have glew32?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM