First of all, this thread should answer all your questions about what a DLL is and what is meant by "dynamic-link".
Now, for a simple example of a DLL, here is the simplest example I can think of, the "hello world" program:
In hello_world.h:
#ifndef HELLO_WORLD_H
#define HELLO_WORLD_H
#ifdef COMPILING_DLL
#define DLL_FUNCTION extern "C" __declspec(dllexport)
#else
#define DLL_FUNCTION extern "C" __declspec(dllimport)
#endif
DLL_FUNCTION void printHelloWorld();
#endif
In hello_world.cpp:
#define COMPILING_DLL
#include "hello_world.h"
#include <iostream>
DLL_FUNCTION void printHelloWorld() {
std::cout << "Hello World!" << std::endl;
};
In main.cpp:
#include "hello_world.h"
int main() {
printHelloWorld();
return 0;
};
Now, you must compile the "hello_world.cpp" as a DLL and then compile the "main.cpp" as an executable, linking it with the import library of the DLL.
If you are using MinGW GCC, then you can do this:
g++ hello_world.cpp -shared -o hello_world.dll --out-implib libhello_world.a
g++ main.c -o main.exe -lhello_world
If you are using MSVC, then you should just create one "DLL" project with the "hello_world.cpp" file as the source file. And then, create one executable project with the main.cpp file. You need to first compile the DLL project (as "hello_world.dll"), then add the "hello_world.lib" to your link-libraries in your executable's project settings (build options), and then compile the executable. Or, equivalently in command-line:
cl -o hello_world.dll hello_world.cpp /link /DLL
cl -o main.exe main.cpp hello_world.lib
I'm pretty sure the above will work.