28

To use FreeGlut librarie function, I had to do the following,

  • Added freeglut.h as a Header File
  • Added freeGlut.lib as a Resources File
  • Copied freeGlut.dll to my windows/SysWOW64 folder

But, how this whole system(.h, .lib & .dll) is interrelated with each other?

I know, the most basic thing is add a header file with class declaration and write the body on its respective source file. Finally include the header file to you main app.

Quazi Irfan
  • 2,555
  • 5
  • 34
  • 69
  • possible duplication: http://stackoverflow.com/q/924485/1441 (What's the difference between a header file and a library?) et. al. – crashmstr Jun 20 '11 at 18:53

3 Answers3

25

You have to include the header files so that the compiler understands the declarations of various types and functions that are exposed/used by the library.

The library files(lib or dll) contain the object code to which the code using the library links to.

for lib the linkage is static and happens at compile time.Using a static library(lib) causes the size of your executable to increase because the entire lib is linked in to your program.

for dll the linkage is dynamic and calls are evaluated at runtime.When using dll the size of your executable does not increase because the dll's are linked at runtime and you usually need to place them at predefined paths so that they can be linked at runtime.

Advantage of static library over dll is that your executable which uses the lib is standalone while in case of dll, the dll needs to be present at a predefined path whil running the executable which uses it.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • but, the lib and dll are both in place, where the request is going to? – Quazi Irfan Jun 20 '11 at 18:51
  • and in case of freeGlut, is has one freeGlut.lib & freeGlut_static.lib. It says for static use, use freeGlut_static.lib. It contradicts with what you said about lib. – Quazi Irfan Jun 20 '11 at 18:53
  • 31
    Sometimes the lib contains the full implementation, other times it just contains stubs that call out to the DLL. Only if it contains the full implementation is it a static link. – Mark Ransom Jun 20 '11 at 18:54
  • 1
    is it possible for a lib to contain a partial implementation? and redirecting to its respective dll for the rest call. – Quazi Irfan Jun 20 '11 at 19:03
  • One more thing, I remove the freeGlut.dll and freeGlut.lib, then added freeGlut_static.lib. But, program says, freeGlut.lib not found! Why he should ask for freeGlue.lib? The static lib is added to the project's Resource. – Quazi Irfan Jun 20 '11 at 19:22
  • 1
    if you replace dynamic libs with a static lib (or vice versa) and you only do a BUILD then the build utility sees that your source haven't changed ,so it won't do a compile but just a link. You need a REBUILD (compiling and linking). You will probably also need to specify a define in your source(s) or in you compile-command to tell which version you want to use (then a BUILD suffice). – engf-010 Jun 20 '11 at 23:36
  • @edwin What to define? Why would I even need to define something? – Quazi Irfan Jun 21 '11 at 11:00
  • 1
    @iamcreasy: you need to look in the header-file (or other documentation) to see which ifdef branch is compiled for one particular situation and add or remove (there may be a default) the definition accordingly. – engf-010 Jun 21 '11 at 14:50
  • 1
    @iamcreasy: In most libraries there is one header-file for both situation and these are controled by the presence or absence of a particular defined name. Sometimes there might be two header-files ,one for each situation ,in this case don't have to define anything since you choose it explicitly through including the right header-file. – engf-010 Jun 21 '11 at 14:59
  • thanks, Ill keep an eye on things, these really gives a lot of pain when someone dont know the details. – Quazi Irfan Jun 21 '11 at 16:47
21

Header file declares everything so that the compiler knows that you will provide it.

The .lib file is used to tell the linker where to find the implementations- normally they're just packaged in the .lib itself but in this case you have an import library, so it says "In the .DLL that I will dynamically load at run-time".

The .dll file is the implementation and is loaded at run-time. Normally you would not copy this to your System folder but instead leave them in the folder with your .exe.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    Doesn't the lib layer looks like a burden, unless it already contains the code? Is it just for redirecting request to a dll file? or does it serve some other purpose? – Quazi Irfan Jun 20 '11 at 18:56
  • 1
    No, it can either contain code implementation (as in your freeglut_static.lib) or DLL import references (as in freeglut.lib) or even a mix of both. A .lib is just a collection of .obj files packaged up for convenience. In the DLL import case you do need some data to identify where the function should be imported from, yes, so without putting all this metadata elsewhere (e.g. as pragmas in your header file) there's no way to skip it. – Rup Jun 20 '11 at 19:00
3

The .h file is necessary for the compile step: it declares the set of functions, structures and signatures that are available in FreeGLUT so that the compiler can code against them.

The .lib file is necessary for the link step: at this point you'll have a set of object files that reference the FreeGLUT functions but no actual definition / implementation of them. In this case, it'll define them as DLL imports from the FreeGLUT.DLL that get resolved at load time.

The .dll file is where the code actually lives and is necessary for run-time. It needs to go somewhere in your path, or in the directory that you're running your code from, so that Windows can find it. Note that SysWOW64 is almost certainly the wrong place for it; you either need to set up Visual Studio to copy it into your bin\debug directory or put it somewhere else and add that directory to your PATH environment (then restart Visual Studio to pick it up).

Rup
  • 33,765
  • 9
  • 83
  • 112
  • Thanks for your answer. But, can you explain a little more about the "link step" and "object file"? – Quazi Irfan Jun 20 '11 at 19:02
  • 1
    Traditionally (although the line is a little blurred nowadays) what happens is that your individual .cpp files are first compiled into a collection of .obj files (the object files - you'll see these in your obj\Debug directory) which contain machine code plus lists of imported and exported functions that are used to knit the individual .obj files together into the complete program. That's [linking](http://en.wikipedia.org/wiki/Linker_(computing)). As well as your obj files your code will be linked against the C++ runtime library to import implementations of standard functions etc. – Rup Jun 20 '11 at 19:08
  • so lib file is actually a collection / package of obj files which are generated form the source code. And, link step checks is the code exists or he need to jump to another dll. – Quazi Irfan Jun 20 '11 at 19:16