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.

1

3 Answers 3

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.

10
  • 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. Jun 20, 2011 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. Jun 20, 2011 at 19:03
  • 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, 2011 at 23:36
  • 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, 2011 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, 2011 at 14:59
20

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.

2
  • 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? Jun 20, 2011 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, 2011 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).

3
  • Thanks for your answer. But, can you explain a little more about the "link step" and "object file"? Jun 20, 2011 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. 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, 2011 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. Jun 20, 2011 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.