Santosh's answer is correct (expect for extern: it is not necessary if you provide the .h files from your dll project).
I will just explain a little bit more about
statically linking the dll:
First you should declare your functions properly in your dll project. If this doesn't already exist in your project, I suggest you add a file (for example
ImportExport.h) with the following defines:
#ifdef YOURDLL_EXPORTS
#define YOURDLL_API __declspec(dllexport)
#else
#define YOURDLL_API __declspec(dllimport)
#endif
And go to your project properties,
C/C++,
Preprocessor, in the
Preprocessor Definitions, add
YOURDLL_EXPORTS
to the list. Do that for all configurations.
Now inside your dll project, modify all your class and functions definitions using
YOURDLL_API
to export them (you need to include
ImportExport.h:
class YOURDLL_API MyClass { ... };
YOURDLL_API void MyFunction();
The
ImportExport.h technic is widely used and makes maintenance much easier.
After that, after compilation, 2 important files will be produced:
- the
.dll file (needed at runtime)
- the
.lib file (needed at compilation time by the application that will use the dll)
Make sure you have the .lib file before continuing. If you can't find it, check your project properties. You can find its path under,
Linker,
Advanced,
Import Library.
After compilation, you can make sure all exports are OK with a very useful tool:
http://www.dependencywalker.com/[
^]
Use that tool to check that all your functions/classes are properly exported.
Now, to use the dll in your other projects:
- for coding, use the necessary
.h files from your dll projects to code whatever you want
- DO NOT define
YOURDLL_EXPORTS in your project, so the compile will
import these functions automatically (so we don't need to declare the functions as
extern
).
- to make project compile, you need to give the
.lib file to the compiler. Go to the projet properties,
Linker,
Input, and add your .lib file in the
Additional Dependencies field. Don't forget to do that for all configurations (Debug, Release, ...).
To run the application, don't forget to copy the dll next to the final exe.