Tuesday, 24 March 2009

Headaches With MinGW and __stdcall

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

Friday, 13 March 2009

Elbow at Newport

Las night I was at an Elbow gig in Newport. Why Newport? well that was the only place we could get tickets.

They were playing in the "Newport Center" which, I discovered, was a sports center with seating. Some bands might have worried about such a place giving the wrong impression. Elbow, however, seem to delight in interacting with their paying fans and far from ending up as a low key affair it was better described as "intimate".

Opening with "Starlings" the band came on with trumpets in hand and an accompanying string quartet. The smaller venue put with trade mark mellow opening lulled me into a false sense of security and I removed my ear defenders (just something I always use at every gig).  I did this just in time for the blast of trumpets.

Still there was plenty of time to recover during mirror ball and some riot.
Elbow have a fairly experimental sound at times, beautifully mellow Mancunian vocals with enough electrics (keys, guitar and base) for some to call it rock.

My favorite experience with any live band is the interaction with the audience or crowd.  Guy Garvy is a master for this and has a very quick wit when dealing with hecklers (you know who you are. Dexter).  This is a band that "grew up" to stardom by simply getting better. And nothing else. Their fan base has grown, but they are as down to earth now as they ever could have been.

Some bands albums are so manicured by the sound engineer that seeing them live is a disappointment.  Elbow are not one of bands and despite the high standard of their studio albums their music live is entrancingly perfect.

I look forward to seeing them again.