![]() |
| | #1 |
| Guest
Posts: n/a
| .lib vs .h vs .dll |
|
| | #2 |
| Refugee Join Date: Aug 2001
Posts: 2,052
| The headers point to the libs
__________________ Please direct all complaints regarding this post to the nearest brick wall Have a nice day. |
| face_master is offline | |
| | #3 |
| Programming Sex-God 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. |
| Polymorphic OOP is offline | |
| | #4 |
| Guest
Posts: n/a
| 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 |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| Thanks. At least someone doesn't hate me on these boards. |
| Polymorphic OOP is offline | |
| | #7 | |
| Guest
Posts: n/a
| Quote:
| |
|
| | #8 |
| Banned Join Date: Oct 2001
Posts: 1,552
| 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 |
| frenchfry164 is offline | |
| | #9 |
| mustang benny 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?
__________________ benforbes@optusnet.com.au Microsoft Visual Studio .NET 2003 Enterprise Architect Windows XP Pro Code Tags Programming FAQ Tutorials |
| bennyandthejets is offline | |
| | #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. |
| Imperito is offline | |
| | #11 |
| mustang benny 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 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_)
__________________ benforbes@optusnet.com.au Microsoft Visual Studio .NET 2003 Enterprise Architect Windows XP Pro Code Tags Programming FAQ Tutorials |
| bennyandthejets is offline | |
| | #12 | |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,219
| Quote:
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();
};
Code: //MyDll.cpp
#include <windows.h>
#define DLL_EXPORT
#include "MyDll.h"
int MyClass::Hello(){
return MessageBox(0,"Hello World",0,MB_OK);
}
Code: //Test.cpp
#include <windows.h>
#include "MyDll.h"
#pragma comment(lib,"MyDll.lib")
int main()
{
MyClass mc;
mc.Hello();
return 0;
}
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules | |
| Fordy is offline | |
| | #13 |
| mustang benny 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?
__________________ benforbes@optusnet.com.au Microsoft Visual Studio .NET 2003 Enterprise Architect Windows XP Pro Code Tags Programming FAQ Tutorials |
| bennyandthejets is offline | |
| | #14 | |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,219
| Quote:
If you place the dll in either the system folder or the windows folder, then it should be accessable to the whole system
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules | |
| Fordy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| loading .h, .dll, and .lib | sigh | Windows Programming | 4 | 02-08-2008 01:32 AM |
| possible to link and run with .lib file without .dll file? | George2 | Windows Programming | 1 | 05-25-2006 09:05 PM |
| .h and .lib unloader | Killhard666 | C++ Programming | 5 | 09-19-2005 10:20 PM |
| Compile Program missing .lib file have .dll | John Hobbes | C++ Programming | 1 | 11-19-2004 05:52 PM |
| Problem with a .dll file | GaPe | Windows Programming | 2 | 10-29-2003 01:20 PM |