Advertisement
Guest User

Untitled

a guest
Dec 9th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. $ cat host.d
  2.  
  3. import std.exception;
  4. import core.runtime;
  5. import core.sys.posix.dlfcn;
  6. import common;
  7.  
  8. void main(string[] args)
  9. {
  10. auto lib = Runtime.loadLibrary(args[1]).enforce("Failed to load library");
  11. auto getter = cast(Getter*)dlsym(lib, "getInstance").enforce("Failed to get `getInstance`");
  12. auto instance = (*getter)();
  13. instance.foo();
  14. }
  15.  
  16. $ cat a.d
  17. import common;
  18. class Baz : Bar
  19. {
  20. override void foo()
  21. {
  22. import std.stdio;
  23. writeln("It works!");
  24. }
  25. }
  26.  
  27. extern(C) Bar getInstance() { return new Baz; }
  28.  
  29. $ cat common.d
  30. interface Bar
  31. {
  32. void foo();
  33. }
  34.  
  35. extern(C) Bar getInstanceDL();
  36. alias Getter = typeof(getInstanceDL);
  37.  
  38. $ cat build.sh
  39. # NOTE: you must create a symlink the shared version of Phobos or update the
  40. # rpath references appropriately
  41. DC="dmd"
  42.  
  43. # Host
  44. $DC -g host.d common.d -L-ldl -map -defaultlib=libphobos2.so -L-rpath=.
  45.  
  46. # a.so
  47. $DC -c a.d -fPIC -ofa.o common.d
  48. $DC -ofa.so a.o -shared -defaultlib=libphobos2.so -L-rpath=.
  49.  
  50. $./build.sh
  51. $./host "$PWD/a.so"
  52. Fatal Error while loading '/home/justin/workspace/aiasflow/tests/interfaces/a.so':
  53. The module 'common' is already defined in './host'.
  54. Segmentation fault
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement