• entries
146
436
• views
198529

# Dynamic Link Libraries

163 views

You know, every time I've sat down to write my next entry, something (or someone) always comes up and bugs me. I hate distractions. Anyways, before I resume my discussion on OOP, I thought I would cover a topic that came up recently, dynamic link libraries.

Why would you use a DLL?
Quite simply, a DLL allows you to separate components of your engine/game from each other. This has many consequences for a game, some of which I'll list here. By using DLLs you introduce the ability to update one part of the engine without affecting any of the other components. They also provide a level of logical separation that is much harder to break, this makes it easier to decouple things from each other, and thus enable the aforementioned ability to update without having to rebuild the entire game. Finally, they give you the ability to enable mod makers to just substitute their own DLL in place of the game logic you provided. You can see such behavior in the original Half-Life engine (with perhaps the most mods, and most popular ones too).

Signatures
When you are writing a DLL you need to export symbols from it so that others applications can load up the DLL and access those functions and objects that you wish them to use. There are some problems though, and the first deals with the compiler and how it names functions. When you build a program, the function names are mangled to ensure that they are unique, and in the case of C++, to provide a unique signature to the overloads that can be identified by the compiler easily.
int f(int); //Becomes: ?f@@YAHH@Z double f(double); //Becomes: ?f@@YANN@Z

As you can see, these two overloaded functions have different signatures. As to the exact meaning of the various symbols, that's up to the compiler vendor. However, in this case (VS2005) we see that H stands for int, and that it marks it as both returning an integer and taking an integer. N stands for double. This means that if you were to export these two functions from a DLL, then the compiler used to link to that DLL would also have to mangle the function names in exactly the same way. Otherwise when it went to look up the function name in the DLL, it wouldn't find it, since the two names would not match.

C is different however, it does specify how names should be mangled, by appending an underscore to the start of the function name. Something to note however: C does not support overloads. So if you do decide to mark a function as using the C calling convention, then you cannot overload it. The typical way we mark a function as using the C calling convention is to use the extern keyword.
extern "C" int f(int);

Now the mangled name of this function is _f. This name does not depend on the compiler, and thus a DLL exporting this name will be able to be used by many different applications compiled using many different compilers.

Next time, exporting functions from a DLL.

## 1 Comment

You've got my attention. Hurry up with the next one!

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account