Original Post
Hello. With my engine I like to load every external shared library at runtime, as to provide the different codepaths based on what libraries the user has installed. Consider the following example that illustrates what I'm doing: Now, this wouldn't be anything special except I'm not linking zlib to my program at all. Here's how I do it: So, my question is, while it's a piece of cake to do on a GNU/Linux system (or any other UNIX-like system for that matter), I have absolutely no idea how can this be achived on Windows. I tried doing this on MigGW, but without success. Any help?
#include <stdio.h>
#include <dlfcn.h>
#include <zlib.h>
int main( )
{
void * library = dlopen( "libz.so", RTLD_LAZY | RTLD_GLOBAL );
if( library == 0 )
printf( "zlib is not present on the system\n" );
else
printf( "%s\n", zlibVersion( ) );
return 0;
}
gcc -fpic -pie example.c -ldl -Wl,--unresolved-symbols=ignore-allIt's all done at runtime. The linker automatically binds the symbols from dynamically loaded library to global symbols used by the program. This saves me the trouble of manually creating abominations like this:
typedef const char * ( *zlibVersion_TYPE )( );
zlibVersion_TYPE zlibVersion_BINDING;
bool load_zlib( )
{
(...)
zlibVersion_BINDING = dlsym( library, "zlibVersion" );
(...)
}
const char * zlibVersion( )
{
return zlibVersion_BINDING( );
}