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.

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.

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.

That sounds very similar to a *.h file in C++. Is the difference between then that .lib files are not recompiled every time you compile the project? Also, when using APIs like DirectX, I find I often have to #include a file like dinput8.h, and then also link to a .lib file like dinput8.lib. Why is that? And are .libs language specific, like given any lib, could two different programing languages use it (not simultaneously, just in general)?

How you create DLLs and libs depend on the compiler you use. Each compiler does it differently.

I'm using VC++ 2008 Express.

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.

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.

I see. So is there anytime when you would have .dll file, but no accompanying .lib file, or visa versa?

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.

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?

The *.lib files generally can not be used with other languages -- they often can't even be used by other compilers.

So, what about the .lib files included with DirectX? Did they just happen to be compiled with VC++?

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 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.

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?

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.

So, how would you use/access a DLL that doesn't have a .lib paired with it? And what is a static library?

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?

>> 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.

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?

Yes, that is right. The speed difference, however, is very minal on modern lightning fast computers.

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.

Q2) You place DLLs in one of the directories that are in your computer's PATH environment variable. If you don't know what that is then create a cmd.com command prompt and type PATH <Enter>.

I'll look some more at your Q1 and let you know later, unless someone else posts the answer before I get around to it.

Q1: It worked for me using VC++ 2008 Express. I creaed a header file named myheader.h and put it in the same directory as all the other compiler's header files

#ifndef MYHEADER_H
#define MYHEADER_H

void foo(const char* p1, int i1);

#endif

Then I created a new *.cpp file. As soon as I typed foo( as shown below the IDE displayed the function and all its parameters.

#include <myheader.h>
#include <iostream>
using namespace std;

int main()
{
    foo(

@Q2: Thanks, that did it.

@Q1: Would that fact that I use __declspec(dllexport) in front of my functions do anything?

I checked, and when I removed that, it worked, but when I put them back, it still worked. I think the IDE just needed time or something... It works now.

This article has been dead for over six months. Start a new discussion instead.