How do I load a shared library?
Nafees via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 8 00:33:30 PDT 2016
I've seen the page on how to load/make Shared Libraries, but it
doesn't work as mentioned here
https://dlang.org/dll-linux.html#dso10
I have 2 files:
lib.d contains:<code>
import core.stdc.stdio;
extern (C) int dll()
{
printf("dll()\n");
return 0;
}
shared static this()
{
printf("libdll.so shared static this\n");
}
shared static ~this()
{
printf("libdll.so shared static ~this\n");
}
</code>
and loader.d contains:<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("/home/nafees/Desktop/temp/libdll.so",
RTLD_LAZY); //The path is absolutely correct
if (!lh)
{
fprintf(stderr, "dlopen error: %s\n", dlerror());
exit(1);
}
printf("libdll.so is loaded\n");
int function() fn = cast(int function())dlsym(lh, "dll");
char* error = dlerror();
if (error)
{
fprintf(stderr, "dlsym error: %s\n", error);
exit(1);
}
printf("dll() function is found\n");
fn();
printf("unloading libdll.so\n");
dlclose(lh);
printf("-main()\n");
return 0;
}
shared static this() { printf("main shared static this\n"); }
shared static ~this() { printf("main shared static ~this\n"); }
</code>
I compile lib.d using the -shared & -m32 switches, and loader.d
with -m32 switch (I want the result to be n 32 bit). It compiles
fine, but when I run loader, it crashes as soon as dlopen is
called, giving a segFault.
I am using latest DMD, on xubuntu 16.04.
More information about the Digitalmars-d-learn
mailing list