Dynamically calling external libraries.

Setra roederharpo at hushmail.com
Thu Feb 27 06:38:10 PST 2014


I have found the Dlang wiki article on how to do it. 
http://dlang.org/dll-linux.html

I am using the following code.

[code]
import core.stdc.stdio;
import core.stdc.stdlib;
import core.sys.posix.dlfcn;

extern (C) int dll();

int main() {
   printf("+main()\n");

   void *lh = dlopen("libdll.so", RTLD_LAZY);
   if (!lh) {
     fprintf(stderr, "dlopen error: %s\n", dlerror());
     exit(1);
   }
   printf("libdll.so is loaded\n");

   int function(int x) fn = cast(int function(int x))dlsym(lh, 
"dll");

   printf("dll() function is found\n");

   fn(72);

   printf("unloading libdll.so\n");
   dlclose(lh);

   printf("-main()\n");
   return 0;
}
[\code]

[code]
import core.stdc.stdio;
import std.stdio;

extern (C) int dll(int x) {
   printf("dll()\n");
	writeln(x);
   return 0;
}
[\code]

I am compiling it the way to guide recomends it. It compiles 
however for some reason when it runs the value returned by the 
dll is incorrect? Does anyone know why? It always returns the 
same value even when I change the parameter.
Thanks!


More information about the Digitalmars-d-learn mailing list