For the last week I’ve been writing a DLL in c++ (using MinGW). I’ve had some real headaches getting information. Since it’s now all working here’s how I got round things.

Calling Conventions
Some languages (including VB6) require all functions to use the stdcall calling convention. Since its common practice to define some sort of constant …
#define EXPORT __declspec(dllexport)
… you might as well use …
#define EXPORT __declspec(dllexport) __stdcall
It just keeps the code clean and simple.

Naming Conventions
Known as Name Mangling; by convention stdcall functions gain a @nn at then end of their names when compiled. So all the dll function names come out like fooBar@4 instead of just fooBar. This can cause problems when using the DLL else where.

MinGW offers the ability to remove these these extra symbols:
g++ -o myDll.dll -shared -Wl,--kill-at fooBar.o

The Problem
As soon as you use -Wl,--kill-at MinGW-built programs using this dll will not compile. They will all throw repeated errors complaining of undefined reference to '_imp__fooBar@4' etc. To correct this you need a .lib file which maps all your functions (fooBar@4) to their name (fooBar) in the dll.

To get a .lib file, some pages tell you that that you need a .def file. The more helpful ones tell you how to create a .def file automatically. They all seem to tell you to manually edit the .def file. Don’t Bother!

The Solution
dlltool shipped with MinGW can read your object files and create a .lib file directly (no .def file needed). The -k will cause the library to map to names without @nn.

dlltool doesn’t actually need to see the dll itself, just the object files that make it. But you will need to tell it the name of the dll (so that this can be included in the lib file). For this use -D dllname.

So to compile a dll (making a lib file for it):
g++ -o myDll.o -c myDll.cpp
g++ -o myDll.dll -shared -Wl,--kill-at myDll.o
dlltool -l myDll.lib -D myDll.dll -k myDll.o

And To build an exe that uses it (-l includes the library):
g++ -o myExe.o -c myExe.cpp
g++ -o myExe.exe myExe.o -lhttpClient