I come from a Delphi world where importing a DLL function statically is pretty easy. All you need to do is specify the function name and the module like this:
function GetTickCount : DWORD; stdcall; external 'Kernel32.dll';
Why in C++ must I use kernel32.lib
in order to import the function? Why can't I simply tell the linker to import that function just like in Delphi?
I know it may sound noobish for manny of you, but coming in the C++ world from Delphi can really get confusing.
The C++ toolchain takes a couple of steps to do what Delphi can do in one step. When declaring an external DLL function in C++, there is no (standard) way to indicate which named DLL the function can actually be found in. As far as the compiler is concerned, the declared function is simply extern
and there must be a definition that can be found somewhere by the linker.
To connect the named function to the DLL in which it can be found, the C++ toolchain requires an "import library" that contains import stubs that the linker knows what to do with. When finding a function defined by an import stub, the linker creates a DLL function reference for the specific function name in the corresponding DLL (as indicated by the import stub).
In Delphi, the language designers allowed the programmer to specify the associated DLL directly in the source code. The Delphi compiler can then directly generate the reference to the external DLL without the use of the import stub step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With