12

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

Peretz
  • 1,096
  • 2
  • 18
  • 31

7 Answers7

4

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    You are wrong saying that "C has always used import libraries". In fact, C has nothing to do with libraries as they are outside of the scope of C standard. This is about linkers and is platform specific. – Maxim Egorushkin Aug 05 '11 at 14:02
  • C or C++ compiler has nothing to do with whatever libraries. It only translates source code into object files. See my updated comment above. – Maxim Egorushkin Aug 05 '11 at 14:06
  • @David: looks better. There are only C linkers though, no need to reinvent the linker for every new language. – Maxim Egorushkin Aug 05 '11 at 14:15
  • @Maxim Why are you so intent on focussing on the nitty gritty of the terminology, rather than the question that was asked, and the answer I gave? Isn't that somewhat more interesting? – David Heffernan Aug 05 '11 at 14:17
  • David, I am not exactly sure the role of an import library. could you further delve into it? also, why C linkers always used import libraries?. Thanks – Peretz Aug 05 '11 at 15:18
  • @Peretz i don't think you need to look beyond the weight of history. Anyway, it is what it is! – David Heffernan Aug 05 '11 at 15:29
3

That is a so-called "import library" that contains minimal wiring that will later (at load time) ask the operating system to load the DLL.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 3
    I think OP knows what it is but rather wonders why it is needed at all. – David Heffernan Aug 05 '11 at 13:36
  • Thanks for the answer. Could you further explain on the minimal wiring the will later later ask the OS to load the DLL? I am not sure of that process. – Peretz Aug 05 '11 at 14:27
2

DLLs are a Windows (MS/Intel) thing. The (generated) lib contains the code needed to call into the DLL and it exposes 'normal' functions to the rest of your App.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Thanks for the answer. Are .Lib a windows only thing too? which termination are cross-platform libraries? – Peretz Aug 05 '11 at 14:29
  • No, `.lib` is more general. But their content is usually very platform specific. – H H Aug 05 '11 at 14:32
2

No, the header file isn't necassarily enough. The header file can contain just the declarations of the functions and classes and other things you need, not their implementations.

There is a world of difference between this code:

void Multiply(int x, int y);

and this code:

void Multiply(int x, int y)
{
   return x * y;
}

The first is a declaration, and the second is a definition or implementation. Usually the first example is put in header files, and the second one is put in .CPP files (If you are creating libraries). If you included a header with the first and didn't link in anything, how is your application supposed to know how to implement Multiply?

Now if you are using header files that contain code that is ALL inlined, then you do not need to link anything. But if even one method is NOT inlined, but has its implementation in a .CPP file that is compiled to a .lib file, than you need to link in the .lib file.

[EDIT] With your use of Import Libraries, you are telling the linker to NOT include the implementation details of the imported code into your binary. Instead the OS will then load the import DLL at run-time into your process. This will make your application smaller, but you have to ship another DLL with it. If the implementation of the library changes, you can just reship another DLL to your customers, and not have to reship the entire application.

There is another option where you can just link in a library and you don't need to ship another DLL. That option is where the Linker will include the implementation into your application, making it bigger in size. If you have to change the implementation details in the imported library, then you have to recompile and relink your entire application, and reship the entire thing to your customers.

C.J.
  • 15,637
  • 9
  • 61
  • 77
  • This is all true, but does not demonstrate the necessity of an import library. – David Heffernan Aug 05 '11 at 13:40
  • oh yes, I forgot about that part of his question... must, edit... reach... for... the... edit... button. :) I'll modify it. – C.J. Aug 05 '11 at 13:42
  • @C, @David: I am a bit confused with the import libraries. If you are telling the linker to not include the implementation detail of the imported code, why care for the implementation at all. Just care later on in run time. I imagine that the reason is to be able to run the debugger or something like that. – Peretz Aug 05 '11 at 14:53
  • @Peretz There's no reason why you should need to use a .lib file in order to link to a DLL with implicit linking. That's just the convention for C linkers on Windows, but there's no need for it to be done that way. It could easily be done with a #pragma or some such implementation specific syntax. – David Heffernan Aug 05 '11 at 14:59
  • This doesn't answer the question of why you need a `.lib` where `.dll` would seem to suffice. – Ruslan Jul 14 '19 at 17:02
1

There are two relevant phases in the building process here:

  • compilation: from the source code to an object file. During the compilation, the compiler needs to know what external things are available, for that one needs a declaration. Declarations designed to be used in several compilation units are grouped in header. So you need the headers for the library.

  • linking: For static libraries, you need the compiled version of the library. For dynamic libraries, in Unix you need the library, in windows, you need the "import library".

You could think that a library could also embed the declarations or the header could include the library which needs to be linked. The first is often done in other languages. The second is sometimes available through pragmas in C and C++, but there is no standard way to do this and would be in conflict with common usage (such as choosing a library among several which provide code variant for the same declarations, for instance debug/release single thread/multithreads). And neither choice correspond well with the simple compilation model of C and C++ which has its roots in the 60's.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • When I link my .lib library, can I statically include it in my project so I do not need to include a DLL later on? how do I do that?. Also, so far I give the project the needed details using .lib file, can I use the DLL file instead of the lib file to provide the implementation details? Thanks – Peretz Aug 05 '11 at 14:47
  • 1
    From the sources, you can either build a .LIB which can be linked statically (and won't be needed at run time) or a .DLL with a companion .LIB (the .LIB will be linked and the .DLL will be needed at run time). – AProgrammer Aug 05 '11 at 14:51
  • Got it, so the .lib file always clarifies the implementation details while developing the code, and then I can decide if it is a static or dynamic library. What would be the equivalent to .lib and .dll files in linux? .so files is equivalent to both? – Peretz Aug 05 '11 at 14:56
  • 1
    .a are equivalent to .lib and .so to .dll. But there is no need of an import library, you feed the .so to the linker and it does the rest. – AProgrammer Aug 05 '11 at 15:07
  • so what is the purpose of .a files? sounds like the .so comprises both .lib and .dll functionality. Can you statically and dynamically link a .so library? – Peretz Aug 05 '11 at 15:14
  • 1
    To link statically (so that nothing is needed at runtime) in Linux, you need the .a, in windows, you need a .lib. To link dynamically (so that the library is needed at runtime but can be changed more easily), you need a .so in linux, and a pair .lib/.dll in windows (the .lib isn't the one needed for static linking). Then you can open a library at runtime (think about plug-in), you just need a .so in linux, and a .dll in windows. – AProgrammer Aug 05 '11 at 15:18
  • Understood, in windows, a .lib library is needed for both static and dynamic linking. That is a funny implementation packaging. It seems simpler the linux one. do you know why is like that? – Peretz Aug 05 '11 at 15:26
1

The header file is consumed by the compiler. It contains all the forward declarations of functions, classes and global variables that will be used. It may also contain some inline function definitions as well.

These are used by the compiler to give it the bare minimum information that it needs to compile your code. It will not contain the implementation details.

However you still need to link in all the function, and variable definitions that you have told the compiler about. Failure to do so will result in a linker error. Often this is contains in other object files which may be joined into a single static library.

In the case of DLLs (or .so files), we still need to tell the linker where in the DLL or shared object the missing symbols are. On windows, this information is contained in a .lib file. This will generate the code to load and link the code at runtime.

On unix the the dll and lib files are combined into a single .so file which you must link against to about linker errors.

You can still use a dll without a .lib file but you will then have to load and link in all the symbols manually using operating system APIs.

doron
  • 27,972
  • 12
  • 65
  • 103
1

from 1000 ft, the lib contains the list of the functions that dll exports and addresses that are needed for the call.

cprogrammer
  • 5,503
  • 3
  • 36
  • 56