Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Delphi linker and C++ linker

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.

like image 548
opc0de Avatar asked Dec 11 '22 21:12

opc0de


1 Answers

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.

like image 100
Greg Hewgill Avatar answered Dec 14 '22 12:12

Greg Hewgill