Last active
August 29, 2015 14:12
-
-
Save MrSmith33/0ef5b7defe10522b1ce1 to your computer and use it in GitHub Desktop.
Linux .so test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module app; | |
import std.stdio : writeln, writefln; | |
import std.string : toStringz; | |
import imodule; | |
version (Posix) | |
{ | |
pragma(lib, "dl"); | |
version (Linux) | |
{ | |
private import std.c.linux.linux; | |
} | |
else | |
{ | |
extern( C ) nothrow | |
{ | |
const int RTLD_NOW = 2; | |
void *dlopen( const( char )* file, int mode ); | |
int dlclose( void* handle ); | |
void *dlsym( void* handle, const( char* ) name ); | |
const( char )* dlerror(); | |
} | |
} | |
alias void* SharedLibHandle; | |
private | |
{ | |
SharedLibHandle LoadSharedLib( string libName ) | |
{ | |
return dlopen( libName.toStringz(), RTLD_NOW ); | |
} | |
void UnloadSharedLib( SharedLibHandle hlib ) | |
{ | |
dlclose( hlib ); | |
} | |
void* GetSymbol( SharedLibHandle hlib, string symbolName ) | |
{ | |
return dlsym( hlib, symbolName.toStringz() ); | |
} | |
string GetErrorStr() | |
{ | |
import std.conv : to; | |
auto err = dlerror(); | |
if( err is null ) | |
return "Uknown Error"; | |
return to!string( err ); | |
} | |
} | |
} | |
IModule loadSharedModule(string file) | |
{ | |
import core.runtime; | |
void* handle = Runtime.loadLibrary(file); | |
if (handle is null) | |
{ | |
writeln("Error loading ", file); | |
GetErrorStr().writeln; | |
return null; | |
} | |
void* farproc = GetSymbol(handle, "getModuleInstance"); | |
if (farproc is null) | |
{ | |
writeln("Error loading symbol getModuleInstance()"); | |
GetErrorStr().writeln; | |
return null; | |
} | |
alias ModuleFactory = IModule function(); | |
return (*cast(ModuleFactory)farproc)(); | |
} | |
void main() | |
{ | |
IModule mod = loadSharedModule("./dll.so"); | |
writeln(mod); | |
mod.name().writeln; | |
writeln("done"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmd -c -ofapp.o app.d imodule.d -debug -g | |
dmd app.o -L-ldl -defaultlib=libphobos2.so | |
rm app.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module dll; | |
import imodule; | |
class Dll : IModule | |
{ | |
override string name() | |
{ | |
return "Dll"; | |
} | |
} | |
export extern(C) IModule getModuleInstance() | |
{ | |
return new Dll; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dmd -c -ofdll.o dll.d -fPIC -g -debug | |
dmd -ofdll.so dll.o -shared -defaultlib=libphobos2.so | |
rm dll.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module imodule; | |
interface IModule | |
{ | |
string name(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment