Q:

c++ dynamic libarary fundamentals

Using GCC 4.6.2 [MinGW]
I am a bit confused about dynamic libraries, I know they have been discussed a lot on SO but none of the previous posts answer my question.
Lets take a simple example:
File 1: main.cpp
#include "function.cpp"

using namespace std;

int main()
{
    display();
    return 0;
}

File 2: function.cpp
#include <iostream>

using namespace std;

void display()  
{
    cout << "testing..." << endl;
}

And then I do the following:
g++ -c main.cpp -o main.o
g++ -c function.cpp -o function.o
g++ -shared function.o -o libfunction.dll
g++ main.o -Lfunction.dll -o result.exe
On doing this the program works.
So in the directory we have files: main.cpp, function.cpp, main.o, function.o, libfunction.dll and result.exe
But then if I were to delete the SHARED LIBRARY libfunction.dll the program still works. How is that possible? I thought the whole idea of shared library
is that the required code to RUN is borrowed during RUN TIME and the absence of libfunction.dll should surely give me an error of the missing library.
For this I have looked into the compiler documentation but did not help

A:

#include "function.cpp" makes all definitions from function.cpp available in main.cpp.
To prevent that, write a header that declares the funcions from function.cpp and include that header in main.cpp. Then compile main.cpp and link against the DLL. See HOWTO Create and Deploy a Sample DLL using MinGW on the MinGW website for details.

A:

You are including function.cpp into the main.cpp file. The definition (body) of the display() function will be compiled and linked along with it.
To experience the expected behavior (and to write sane code), don't #include implementation files. Pull out the declarations into a separate header file, and only include that header file.

