为什么我们需要在Windows C ++中链接kernel32.dll,user32.dll等? [英] Why do we need to link kernel32.dll, user32.dll, etc... in Windows C++?

查看:75
本文介绍了为什么我们需要在Windows C ++中链接kernel32.dll,user32.dll等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,为什么Visual Studio不包含kernel32.dll,user32,dll,winspool.lib等的附加依赖项?

Why does Visual Studio by default include Additional Dependencies of kernel32.dll, user32,dll, winspool.lib, etc...?

为什么这些资源需要链接到Windows上的C ++项目中,为什么它们实际上变成了机器代码并直接插入到每个可执行文件中,还是与可执行文件分开并在运行时在它们之间建立了链接?

Why do these resources need to be linked into C++ projects on Windows and are they actually turned into machine code and inserted directly into each executable or are they kept separate from the executable and a link is made between them at run-time?

推荐答案

为使C ++应用程序在Windows下运行,它至少需要一些系统服务.例如,它需要分配和释放内存,它需要获取被调用的命令行参数,并且需要能够在完成后退出操作系统.通常,它还需要以某种方式接收输入并产生输出,无论是通过GUI还是通过控制台,还是通过网络,或者仅仅是通过在文件系统上读写文件.

In order for a C++ application to run under windows, it needs at least a few system services. For example, it needs to allocate and free memory, it needs to obtain the command line parameters that it was invoked with, and it needs to be able to exit to the operating system when it is done. Usually, it also needs to somehow receive input and produce output, whether it be through a GUI, or through a console, or through the network, or simply by reading and writing files on the filesystem.

魔术没有提供这些服务;其中的每一个都由kernel32.dll,user32.dll等提供.

None of these services is provided by magic; every single one of them is provided by kernel32.dll, user32.dll, etc.

一个常见的误解是,此功能由C和C ++的标准库提供.事实并非如此,因为如果是的话,那么这些库将具有执行魔术的能力.标准库提供的服务是通过委派给主机系统的本机服务来实现的.

It is a common misconception that this functionality is provided by the standard libraries of C and C++. That's not true, because if it was, then these libraries would be capable of performing magic. The services offered by the standard libraries are implemented by delegating to the native services of the host system.

因此,当您调用例如 malloc()时,Windows的标准C/C ++库将在内部调用 GlobalAlloc()(这是在Kernel32中实现的).dll),而MacOS的标准C/C ++库将在内部调用 vm_allocate()或类似的东西.了解这一点非常重要:对于每个不同的主机系统,存在一个不同的标准库实现,这些实现利用了该主机系统的本机服务.

So, when you invoke, say, malloc(), the standard C/C++ library for Windows will internally invoke GlobalAlloc() (which is implemented in Kernel32.dll,) while the standard C/C++ library for MacOS will internally invoke vm_allocate() or something like that. This is very important to understand: for every different host system out there, there exists is a different implementation of the standard libraries, which makes use of that host system's native services.

标准库的好处在于它们建立了一个众所周知的通用接口,您的C/C ++代码可以期望使用该接口,这样您的C/C ++代码就不必知道也不必担心.关于它在其中运行的主机系统的确切信息.

The benefit of the standard libraries lies in the fact that they establish a certain well known common interface that your C/C++ code can expect to have at its disposal so that your C/C++ code does not have to know, nor worry about, exactly what host system it is running in.

在Windows下,您的程序未完全与DLL链接,因为链接是创建可执行文件的过程,而DLL仅在运行时起作用.您的程序与库链接,因此对于每个DLL通常都有一个对应的LIB.例如,对于Kernel32.DLL,存在Kernel32.LIB,因此您的程序将与Kernel32.LIB链接.

Under Windows, your program does not exactly get linked with DLLs, because linking is the process of creating an executable, while DLLs only come into play at runtime. Your program gets linked with libraries, so for each DLL there is usually a corresponding LIB. For example, for Kernel32.DLL there is Kernel32.LIB, so your program gets linked with Kernel32.LIB.

这些LIB很小,因为它们不包含实际的代码.程序运行时Kernel32.LIB的作用是要求存在相应的Kernel32.DLL,它要求操作系统加载Kernel32.DLL(要求将其映射到进程的内存空间的可能性更大,(因为通常已经加载了Kernel32.DLL),然后它将每个库调用重定向到DLL的相应入口点.

These LIBs are tiny, because they do not contain the actual code. What Kernel32.LIB does when your program runs is that it demands that the corresponding Kernel32.DLL be present, it asks the operating system to load Kernel32.DLL, (asks for it to be mapped onto the memory space of your process more likely, since Kernel32.DLL will generally already be loaded,) and it then redirects every single library call to the corresponding entry point of the DLL.

是的,每个C ++程序都需要使用这些DLL,否,它不会完全链接DLL本身,它仅链接相应的LIB,然后将调用委托给DLL,而LIB很小,所以不用担心它们.

So, yes, every single C++ program needs to make use these DLLs, no, it does not exactly link the DLLs themselves, it only links the corresponding LIBs, which then delegate calls to the DLLs, and the LIBs are tiny, so don't worry about them.

请注意,从理论上讲,有可能编写一个自包含程序,从而不需要任何这些DLL,但是这样的程序实际上将无法执行任何操作:加载后,它将被限制为除了能够进行内省性思考外,什么也做不了,却无法接收任何输入,也不会产生任何输出.

Note that it may theoretically be possible to write a program that would be so self contained as to not need any of these DLLs, but such a program would not be able to do virtually anything: once loaded, it would be restricted to doing nothing but introspective thinking on its own, without being able to receive any input, nor produce any output.

这篇关于为什么我们需要在Windows C ++中链接kernel32.dll,user32.dll等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆