简体   繁体   中英

Why do we need to link kernel32.dll, user32.dll, etc… in Windows C++?

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

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?

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.

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

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.

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.

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM