Jump to content
  • Advertisement
Sign in to follow this  
Hnefi

Multiplatform dynamic library library for C/C++?

This topic is 3802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm developing a game, a central component of which is extensibility by third parties. Basically, I want to let the user create dynamic libraries and load them into the game at runtime. I want to keep the interface as simple and multilanguage compatible as possible; preferrably just plain C functions and variables. It is my understanding that staying away from C++ constructs make it possible to link to libraries created in other compiled languages without any extra effort. Trouble is, I want to support both Linux and Windows platforms. I suppose I could write a wrapper class for loading the necessary functions, but I'd rather not. Does anyone know of a nice and stable multiplatform dynamic library loader for C/C++?

Share this post


Link to post
Share on other sites
Advertisement
Thanks, but wxWidgets is absolutely humongous. I don't want a dependency to such a huge library just to load .dll and .so files. Do you know of any smaller alternatives?

Share this post


Link to post
Share on other sites
Quote:
Original post by Hnefi
I want to support both Linux and Windows platforms. I suppose I could write a wrapper class for loading the necessary functions, but I'd rather not. Does anyone know of a nice and stable multiplatform dynamic library loader for C/C++?

I'm not aware of anything that isn't part of a larger framework (I don't think even boost has something for this yet, although there have been discussion papers at the C++ standard committee).

However, it's not rocket science to write a wrapper class around LoadLibrary/dl_open/dylib_whatever for a platform-independant API. You ABI will still be platform-specific, but that shouldn't matter too much since so will the libraries and apps.

Share this post


Link to post
Share on other sites
You can provide a somewhat platform-independent interface, but the actual code will have to be rewritten for each platform. Here is what I used for a while (win32 version):

In a shared (between engine and dynamic libraries) .h file:
// base interface class
class moduleInterface
{
public:
virtual bool Init( void ) = 0;
virtual void Shutdown( void ) = 0;
};

// and then you have the actual class used by the libraries..
class renderInterface: public moduleInterface
{
// ...
}


In the engine:
typedef moduleInterface *(*functionPtr)(moduleInterface **Interface);

bool openModule( const std::string& moduleName, size_t *libHandle, moduleInterface **interfacePtr )
{
bool success = false;

*interfacePtr = NULL;
std::string modulestr = moduleName + ".dll";
HMODULE libraryHandle = LoadLibrary( modulestr.c_str() );
if ( libraryHandle ) {
*libHandle = reinterpret_cast<native_t>( libraryHandle );
functionPtr CreateInterface = reinterpret_cast< functionPtr >( GetProcAddress( libraryHandle, "CreateInterface" ) );
if ( CreateInterface ) {
CreateInterface( interfacePtr );
if ( *interfacePtr ) {
success = true;
}
}
}

if ( !success ) {
FreeLibrary( libraryHandle );
}
return success;
}

void closeModule( size_t libHandle, moduleInterface **interfacePtr )
{
assert( libHandle );
assert( interfacePtr && *interfacePtr );
HMODULE libraryHandle = reinterpret_cast<HMODULE>( libHandle );
assert( libraryHandle );
functionPtr DestroyInterface = reinterpret_cast< functionPtr >( GetProcAddress( libraryHandle, "DestroyInterface" ) );
if ( DestroyInterface ) {
DestroyInterface( interfacePtr );
}
FreeLibrary( libraryHandle );
}


In the DLL:
// actual library class being imported
class rendererD3D
{
// ...
}

// required module functions
void CreateInterface( renderInterface **Interface )
{
if ( !*Interface ) {
*Interface = new rendererD3D;
}
}
void DestroyInterface( renderInterface **Interface )
{
if ( *Interface ) {
delete *Interface;
*Interface = NULL;
}
}


And then to use it:
renderInterface *renderLib;
size_t libraryHandle; // generic library handle holder
if ( !openModule( "renderer", &libraryHandle, reinterpret_cast<moduleInterface**>(&renderLib) ) ) {
throw ExceptionError( "Unable to load renderer module." );
}

Share this post


Link to post
Share on other sites
Quote:
Basically, I want to let the user create dynamic libraries and load them into the game at runtime.


Use a scripting language perhaps?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!