Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do I need to link a lib file to my project?

Tags:

c++

c

dll

I am creating a project that uses a DLL. To build my project, I need to include a header file, and a lib file. Why do I need to include the respective lib file? shouldn't the header file declare all the needed information and then at runtime load any needed library/dll?

Thanks

like image 627
Peretz Avatar asked Aug 05 '11 13:08

Peretz


Video Answer


1 Answers

In many other languages, the equivalent of the header file is all you need. But the common C linkers on Windows have always used import libraries, C++ linkers followed suit, and it's probably too late to change.

As a thought experiment, one could imagine syntax like this:

__declspec(dllimport, "kernel32") void __stdcall  Sleep(DWORD dwMilliseconds);

Armed with that information the compiler/linker tool chain could do the rest.

As a further example, in Delphi one would import this function, using implicit linking, like so:

procedure Sleep(dwMilliseconds: DWORD); stdcall; external 'kernel32';

which just goes to show that import libraries are not, a priori, essential for linking to DLLs.

like image 166
David Heffernan Avatar answered Oct 20 '22 20:10

David Heffernan