Thread: .lib vs .h vs .dll

  1. #1
    Shadow12345
    Guest

    .lib vs .h vs .dll

    This is going to sound like a painfully stupid question because I should already know the answer, but exactly how are those files related, meaning what does each contain? The header files contain prototypes correct? Dlls actually contain the functions that are being called correct? But if that is true how the heck do the .lib files fit into the equation and why are the necessary?/

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    The headers point to the libs

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    a lib is a static link library and a dll is a dynamic link library. There is nothing in C++ that deals with dynamic linking (implementation varies between OSs and compilers), so it's tough to go into to many details that apply to all types of dynamic link libraries, but here goes:

    A DLL is an executable -- not too dissamilar from an exe file. It contains functions, etc. and has a link phase just like an executable. You can even create a function for it that is executed on its load (similar to main). A DLL can be loaded by many programs -- each instance has its own set of variables associated with it, like when you open up an executable several times. The concept with a DLL is you can load a DLL at loadtime or dynamically during runtime and call functions that are defined within it from the executable that loads it. You'd use it for things that are hardware specific (drivers), that have to have a general interface but implementation may vary and may need to be updated. So the program that uses the DLL opens up the DLL and uses functions within it. Then, all you have to do is replace the DLL and you change how function is implemented. For instance, let's say you had a function in a DLL called "print" that makes your printer print a document. Different printers might require different code to perform the task. If you get a new printer, etc. all you have to do is get a new DLL. Then, any programs that use the DLL to print work with the updated version and all you had to do was update one file without having to touch the data in all of the other programs. You can also use a DLL, for instance, to hold all of your code used for actually renderign a 3D scene. In one DLL you might have implementation for rendering with OpenGL while in another DLL you'd have information for rendering with Direct3D. Then, to switch implementation, you just switch during runtime which DLL you are linked with.

    A static link library has no link phase. It containes compiled functions, etc. that you'd link with an executable as you build it. When you link to a static link library, the code within it is actually stored in your executable. That means that any programs which use the library have a copy of all of the functions within the program that uses them (unlike when you dynamically link to a DLL). Static link libraries make updating your software harder to deal with because you'd often times have to completely replace the executable.

    When you create and use a DLL in windows, you generally have 2 options -- you can use loadtime linking or runtime linking. With loadtime linking, you usually actually use a static link library which contains the information on how to link with the DLL and the DLL is automatically loaded at load time and closed on exit. When you use the functions in the DLL you don't have to change much in terms of implementation (except for your headers). Runtime linking takes a little more work and requires you to explicitly load the library, but it, of course, gives you the freedom of using different DLL's with the same function declarations.

    So you use loadtime linking generally for when you are just using DLLs for updating your software and you use runtime linking when you'd be doing things like the OpenGL/Direct3D example I gave earlier or if you were using the DLL like a mod for a game (ala half-life) where each mod is in the form of a DLL and you'd want to choose which mod you were playing dynamically.

    Header files are used to tell your program that a function exists even though its implementation may not be visible at compile time (and can also contain data telling it that the information won't even be there during the link phase if it's a DLL). So you'd use a header in conjunction with a static link library or dynamic link library to say "even though we can't directly see the code for this funciton that you are calling, we're going to pretend like it exists for now." Then, when the program enters the link phase of building your project, it links with the static link library (if it's a lib) and when you actually run your program, it links with functions, etc. in a DLL if you chose to dynamically link with one.
    Last edited by Polymorphic OOP; 12-16-2002 at 08:47 PM.

  4. #4
    Shadow12345
    Guest
    I was re-reading this post Poly and I realized I never thanked you for posting it. Man dude, like, you just go crazy with your explanations (which I'm extremely grateful for). You need to open up your own site with tutorials and such becuase you explain things really really well, first the bsp, then this, and a range of things in between.

    Thanks Poly.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Thanks. At least someone doesn't hate me on these boards.


  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    There is no hate... only a feeling of inferiority
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Shadow12345
    Guest
    Thanks. At least someone doesn't hate me on these boards.
    well I don't see how anyone can hate someone they don't really know. For all I know poly, you and I could have conflicting personalities and might beat the crap out of each other if we met in person. With that said and done I automatically accept mysterious online figures who are willing to share their (in this case extensive) knowledge.

  8. #8
    you really should make a website. you know your stuff.

    I don't hate you. I LOVE YOU!!!!!!! j/k

    The only reason I can see for some people to hate you is out of jealousy. You prove them wrong and they get ........ed. I hate people like that. You try to help and they get ........ed.

    PS: I am not gay, notice the j/k

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    okay, so now we all know a lot about libraries and such. but how do we do it? ie, how do we make the dll, and how do we link it into our project?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Making a dll, and loading it into a project, is a very platform dependant thing. For example, the better operating systems don't even call shared libraries DLLs!

    And as far as how to link it, again, that is platform dependant, and also depends on if you want load linking or runtime linking.

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    okay, you want details ill give you details. i use windows xp, VC++ 98, and i compiled a .lib static link library with a function of mine in it. Then i copied the .lib to the lib folder in the Visual Studio folder.

    In another one of my projects i added the library to the linking tab of the project settings, then included in all my source files a header file that declared the function.

    When i try to compile the project, it gives me a linker error.
    Code:
    unresolved external symbol _DoStuff
    DoStuff is the name of the function.

    here is the library source.

    Code:
    //lib.cpp
    #include "stdafx.h"
    
    void DoStuff(HWND hwnd)
    {
    	MessageBox(hwnd,"Call from lib.lib","Warning",MB_OK);
    }
    Code:
    //stdafx.cpp
    #include "stdafx.h"
    Code:
    //stdafx.h
    #if !defined(AFX_STDAFX_H__00001EB2_D2F6_484A_8FA3_0A60F71DD686__INCLUDED_)
    #define AFX_STDAFX_H__00001EB2_D2F6_484A_8FA3_0A60F71DD686__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
    #include <windows.h>
    
    // TODO: reference additional headers your program requires here
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_STDAFX_H__00001EB2_D2F6_484A_8FA3_0A60F71DD686__INCLUDED_)
    is there anything wrong with this code?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    okay, so now we all know a lot about libraries and such. but how do we do it? ie, how do we make the dll, and how do we link it into our project?
    Ok....its been shown before, but I'll show it again...

    MSVC++6 - Go to create a new project and click on Win32 Dynamic Link Library. Now create the following header

    Code:
    //MyDll.h
    #ifndef DLL_EXPORT
    #define DLL_SPEC __declspec(dllimport) 
    #else 
    #define DLL_SPEC __declspec(dllexport)
    #endif
    
    class DLL_SPEC MyClass{
    public:
    	int Hello();
    };
    and the following cpp file

    Code:
    //MyDll.cpp
    #include <windows.h>
    #define DLL_EXPORT
    #include "MyDll.h"
    
    
    
    int MyClass::Hello(){
    	return MessageBox(0,"Hello World",0,MB_OK);
    }
    Now build & go into the project folder for your dll. Inside the debug folder, you have a .lib and a .dll......take them along with MyDll.h from above, create a new exe project, and place them in a new project's folder. Now in your new project, put the following cpp file

    Code:
    //Test.cpp
    #include <windows.h>
    #include "MyDll.h"
    #pragma comment(lib,"MyDll.lib")
    
    
    int main()
    {	
    	MyClass mc;
    
    	mc.Hello();
    
    	return 0;
    }
    Now run it and it should call the "Hello" function....Also...you dont have to export classes...you ca add the DLL_SPEC in a normal function (like void DLL_SPEC foobar();)

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    it works... but id like to avoid doing so much work.

    isn't it possible to have the library modules located in a common folder, so that multiple projects can use them. plus, is it necessary to use the #pragma command? can the libraries be added in the project settings?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    it works... but id like to avoid doing so much work.

    isn't it possible to have the library modules located in a common folder, so that multiple projects can use them. plus, is it necessary to use the #pragma command? can the libraries be added in the project settings?
    The #pragma isnt nessasary...you can add the lib as you always do..

    If you place the dll in either the system folder or the windows folder, then it should be accessable to the whole system

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loading .h, .dll, and .lib
    By sigh in forum Windows Programming
    Replies: 4
    Last Post: 02-08-2008, 01:32 AM
  2. possible to link and run with .lib file without .dll file?
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 05-25-2006, 09:05 PM
  3. .h and .lib unloader
    By Killhard666 in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 10:20 PM
  4. Compile Program missing .lib file have .dll
    By John Hobbes in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2004, 05:52 PM
  5. Problem with a .dll file
    By GaPe in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2003, 01:20 PM