| | |
DLLs and LIBs - what, why, and how?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hey,
So, three questions:
1. What is a DLL and what is a .lib file?
2. (might have been answered w/ #1) What is the purpose of .lib/.dll files?
3. How do I create a .lib file with C++, and (again, may have been answered above) what exactly will this do?
Thanks for any clarifications.
So, three questions:
1. What is a DLL and what is a .lib file?
2. (might have been answered w/ #1) What is the purpose of .lib/.dll files?
3. How do I create a .lib file with C++, and (again, may have been answered above) what exactly will this do?
Thanks for any clarifications.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
A DLL is a library of functions that are shared among other executable programs. Just look in your windows/system32 directory and you will find dozens of them. When your program creates a DLL it also normally creates a lib file so that the application *.exe program can resolve symbols that are declared in the DLL.
A .lib is a library of functions that are statically linked to a program -- they are NOT shared by other programs. Each program that links with a *.lib file has all the code in that file. If you have two programs A.exe and B.exe that link with C.lib then each A and B will both contain the code in C.lib.
How you create DLLs and libs depend on the compiler you use. Each compiler does it differently.
A .lib is a library of functions that are statically linked to a program -- they are NOT shared by other programs. Each program that links with a *.lib file has all the code in that file. If you have two programs A.exe and B.exe that link with C.lib then each A and B will both contain the code in C.lib.
How you create DLLs and libs depend on the compiler you use. Each compiler does it differently.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I'm fairly certain that in order to import c++ code into another language (like Java for example), you will need a dynamic link library in order for the process to be possible.
There are far more reasons for .dll files but I don't know of them yet myself. In fact, the main reason I started practicing c++ a little over a month ago was to learn it for the native methods I was learning from Java.
There are far more reasons for .dll files but I don't know of them yet myself. In fact, the main reason I started practicing c++ a little over a month ago was to learn it for the native methods I was learning from Java.
Last edited by Alex Edwards; Jun 21st, 2008 at 12:40 pm.
•
•
•
•
A .lib is a library of functions that are statically linked to a program -- they are NOT shared by other programs. Each program that links with a *.lib file has all the code in that file. If you have two programs A.exe and B.exe that link with C.lib then each A and B will both contain the code in C.lib.
•
•
•
•
How you create DLLs and libs depend on the compiler you use. Each compiler does it differently.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
DLLs are pre-compiled code. When you compile a dll (or get one from someone else that has been compiled for you) you will get two files: *.dll and *.lib. The *.dll is saved in a directory that is your PATH environment variable, such as c:\windows, and the code in the dll is run when your program is executed. The *.lib file is used by the compiler at link time to resolve all the symbols and function names that are in the dll. Your compiler used the *.lib every time it links your program -- normally every time you compile your program.
Header files -- the ones with *.h extension and most standard c++ headers without extensions -- are completly different than either dll or *.libs. Don't get all these files types confused because each file type has its own unique purpose. If you look in a header file all you will find is 1) #define's 2) function prototypes, 3) structure declarations, and 4) c++ classe declarations. You will normally NOT see any executable code.
>>are .libs language specific, like given any lib
Yes and no. DLLs compiled by C compiler (not c++) can generally be called from other languages. DLLs compiled by c++ can be called by other languages if the c++ functions meet certain criteria, such as no c++ classes or c++ specific code. The *.lib files generally can not be used with other languages -- they often can't even be used by other compilers.
Example: all the MS-Windows DLLs you find in c:\windows\system32 were written by C, not c++. And all (or most) of them can be called by nearly any computer language what will target MS-Windows operating system.
How to create a DLL using VC++ 2008 Express: Very similar to creating any other type of project -- select Win32 Project (not the win32 console project) click Next and you will find an option to create a DLL. Here are google links to more information about this.
Header files -- the ones with *.h extension and most standard c++ headers without extensions -- are completly different than either dll or *.libs. Don't get all these files types confused because each file type has its own unique purpose. If you look in a header file all you will find is 1) #define's 2) function prototypes, 3) structure declarations, and 4) c++ classe declarations. You will normally NOT see any executable code.
>>are .libs language specific, like given any lib
Yes and no. DLLs compiled by C compiler (not c++) can generally be called from other languages. DLLs compiled by c++ can be called by other languages if the c++ functions meet certain criteria, such as no c++ classes or c++ specific code. The *.lib files generally can not be used with other languages -- they often can't even be used by other compilers.
Example: all the MS-Windows DLLs you find in c:\windows\system32 were written by C, not c++. And all (or most) of them can be called by nearly any computer language what will target MS-Windows operating system.
How to create a DLL using VC++ 2008 Express: Very similar to creating any other type of project -- select Win32 Project (not the win32 console project) click Next and you will find an option to create a DLL. Here are google links to more information about this.
Last edited by Ancient Dragon; Jun 21st, 2008 at 8:27 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
DLLs are pre-compiled code. When you compile a dll (or get one from someone else that has been compiled for you) you will get two files: *.dll and *.lib. The *.dll is saved in a directory that is your PATH environment variable, such as c:\windows, and the code in the dll is run when your program is executed. The *.lib file is used by the compiler at link time to resolve all the symbols and function names that are in the dll. Your compiler used the *.lib every time it links your program -- normally every time you compile your program.
•
•
•
•
Yes and no. DLLs compiled by C compiler (not c++) can generally be called from other languages. DLLs compiled by c++ can be called by other languages if the c++ functions meet certain criteria, such as no c++ classes or c++ specific code.
•
•
•
•
The *.lib files generally can not be used with other languages -- they often can't even be used by other compilers.
Ok, so lemme see if I get this so far:
You create a C++ project in some compiler, and set it to compile as a .lib. You compile it. A .dll is created somewhere in C:\windows (some default location?), and a .lib file appears where you would normally find your .exe. Ignoring for a moment how it works internally, linked a second project to this .lib file will give it access to all the functions, classes, etc. that were defined in the code that created the .dll/.lib pair. Is that more or less correct?
And one last thing, when using DirectX, I often find pairs of .lib and .h files (e.g., d3d9.h, d3d9.lib; d3dx9.h, d3dx9.lib). Why is this? Is it so that the compiler knows something about the functions you're linking to in the .lib, so that it can check for errors (eg, passing an int to a function where you should have passed double), or some other reason?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
>>I see. So is there anytime when you would have .dll file, but no accompanying .lib file, or visa versa?
Yes. Some companies publish DLLs without accompanying *.lib. And *.libs have other uses than DLLs. When you create a static library a DLL is not produced.
you probably should read some of these links about mixed-language programming. It can get a little complicated.
>>So, what about the .lib files included with DirectX? Did they just happen to be compiled with VC++?
That is no coincidence. VC++ is a Microsoft product, as well as DirectX. I doubt you can use the same libs that were created for VC++ with other compilers. You might be able to get the same libs compiled with other compilers.
>>You create a C++ project in some compiler, and set it to compile as a .lib. You compile it. A .dll is created
No. You create a c++ project to compile as a DLL, and both the DLL and lib files will be created. The project type must be DLL, not lib. A lib project does not create a dll, but just creates a static library. They are not the same.
>>linked a second project to this .lib file will give it access to all the functions, classes, etc. that were defined in the code that created the .dll/.lib pair. Is that more or less correct?
Yes, thats the general idea.
As I said before, don't confuse the difference between a *.h header file and a lib. Look in your compiler's install/include directory and you will find several hundred *.h files. Some have corresponding *.lib files located in install/lib directory but many do not. You can create header files without creating a library. And you can create a library without creating a corresponding header file.
Yes. Some companies publish DLLs without accompanying *.lib. And *.libs have other uses than DLLs. When you create a static library a DLL is not produced.
•
•
•
•
Interesting... so C code can be interpreted by many different languages, but C++ code can't. Is that only when the code is compressed into a DLL? Like if I were to do the VB equivalent of #include (if such a thing exists) with a .h file using only C code, would it work?
>>So, what about the .lib files included with DirectX? Did they just happen to be compiled with VC++?
That is no coincidence. VC++ is a Microsoft product, as well as DirectX. I doubt you can use the same libs that were created for VC++ with other compilers. You might be able to get the same libs compiled with other compilers.
>>You create a C++ project in some compiler, and set it to compile as a .lib. You compile it. A .dll is created
No. You create a c++ project to compile as a DLL, and both the DLL and lib files will be created. The project type must be DLL, not lib. A lib project does not create a dll, but just creates a static library. They are not the same.
>>linked a second project to this .lib file will give it access to all the functions, classes, etc. that were defined in the code that created the .dll/.lib pair. Is that more or less correct?
Yes, thats the general idea.
As I said before, don't confuse the difference between a *.h header file and a lib. Look in your compiler's install/include directory and you will find several hundred *.h files. Some have corresponding *.lib files located in install/lib directory but many do not. You can create header files without creating a library. And you can create a library without creating a corresponding header file.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
Yes. Some companies publish DLLs without accompanying *.lib. And *.libs have other uses than DLLs. When you create a static library a DLL is not produced.
EDIT: I think I found out what a static library is from Google, is this right:
A static library is linked to after you compile the project but before the executable actually runs. This creates a bigger file, but lets the program run faster than it would with dynamic linking.
Dynamic linking is when the compiler links to a .lib that corresponds to a DLL, and it looks up the function it needs during runtime, as they are called. This results in a smaller, but slower, program.
Is that right?
Last edited by CoolGamer48; Jun 22nd, 2008 at 11:07 am.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
>> So, how would you use/access a DLL that doesn't have a .lib paired with it?
Its called dynamic linking -- see links below. In c++ you would call win32 api function LoadLibrary() to load the dll into memory, then GetProcAddress() to get a function pointer to the function in the library that you want to access.
>>And what is a static library?
I already tried to explain that, but maybe I didn't do a very good job.
Definition of a static library.
Another link here.
Yes, that is right. The speed difference, however, is very minal on modern lightning fast computers.
Its called dynamic linking -- see links below. In c++ you would call win32 api function LoadLibrary() to load the dll into memory, then GetProcAddress() to get a function pointer to the function in the library that you want to access.
>>And what is a static library?
I already tried to explain that, but maybe I didn't do a very good job.
Definition of a static library.
Another link here.
•
•
•
•
EDIT: I think I found out what a static library is from Google, is this right:
A static library is linked to after you compile the project but before the executable actually runs. This creates a bigger file, but lets the program run faster than it would with dynamic linking.
Dynamic linking is when the compiler links to a .lib that corresponds to a DLL, and it looks up the function it needs during runtime, as they are called. This results in a smaller, but slower, program.
Is that right?
Last edited by Ancient Dragon; Jun 22nd, 2008 at 1:02 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Ok, so some quick questions on using my DLL
First one is VC++ specific: If you've ever used APIs (like DirectX) with VC++ (or even if you haven't), you might know about the feature in VC++ that lists the parameters of a function as you type them in a call to the function. So, I have a file, RezNebMath.h (dont ask about the RezNeb part, just a name), which I've compiled into a DLL/LIB combo (along with other files, like a .cpp and a .def), and I added the directory its in to my compilers default search directories for includes, so I #include it with <>, not "". The compilation and link work fine, and the program executes, its just that as I'm typing, VC++ isnt aware that something like Random() is a function (even if I press "Go to deceleration"), yet if I right click on <RezNebMath.h>, and click "Open File", it comes up, and on the first few lines is the prototype for Random(). So anyone know why it doesn't know the function exists until compilation. I know this works for other files that aren't in the project's directory, and included to with <>.
Question number 2 is also VC++ specific, but perhaps less than number 1: I need to have my DLL in the same directory as my source at compilation time in order for it to run after its done compiling. I looked through the list of default search directories for various file types (.h, .lib) and I didn't see a section that I knew was for DLLs. Does anyone know how to set up a default directory to search for DLLs, if possible.
Thanks.
First one is VC++ specific: If you've ever used APIs (like DirectX) with VC++ (or even if you haven't), you might know about the feature in VC++ that lists the parameters of a function as you type them in a call to the function. So, I have a file, RezNebMath.h (dont ask about the RezNeb part, just a name), which I've compiled into a DLL/LIB combo (along with other files, like a .cpp and a .def), and I added the directory its in to my compilers default search directories for includes, so I #include it with <>, not "". The compilation and link work fine, and the program executes, its just that as I'm typing, VC++ isnt aware that something like Random() is a function (even if I press "Go to deceleration"), yet if I right click on <RezNebMath.h>, and click "Open File", it comes up, and on the first few lines is the prototype for Random(). So anyone know why it doesn't know the function exists until compilation. I know this works for other files that aren't in the project's directory, and included to with <>.
Question number 2 is also VC++ specific, but perhaps less than number 1: I need to have my DLL in the same directory as my source at compilation time in order for it to run after its done compiling. I looked through the list of default search directories for various file types (.h, .lib) and I didn't see a section that I knew was for DLLs. Does anyone know how to set up a default directory to search for DLLs, if possible.
Thanks.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
![]() |
Similar Threads
- Parents/Children - Processes/Threads (C++)
- need a help regarding "setup" in pyton using py2exe (Python)
Other Threads in the C++ Forum
- Previous Thread: graphics problem
- Next Thread: c++ Beginner Error
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






