www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I load a shared library?

reply Nafees <nafees.hassan outlook.com> writes:
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.
Oct 08 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 8 October 2016 at 07:33:30 UTC, Nafees wrote:
 It compiles fine, but when I run loader, it crashes as soon as 
 dlopen is called, giving a segFault.
What output do you get? If you compile with `-g` to dmd and run it in gdb, you can also use the command `where` to gdb and get a file/line number for the segfault. What does it say?
Oct 08 2016
parent reply Nafees <nafees.hassan outlook.com> writes:
On Saturday, 8 October 2016 at 13:46:50 UTC, Adam D. Ruppe wrote:
 On Saturday, 8 October 2016 at 07:33:30 UTC, Nafees wrote:
 It compiles fine, but when I run loader, it crashes as soon as 
 dlopen is called, giving a segFault.
What output do you get? If you compile with `-g` to dmd and run it in gdb, you can also use the command `where` to gdb and get a file/line number for the segfault. What does it say?
http://imgur.com/U9ZYhVKl.png
Oct 08 2016
parent reply Nafees <nafees.hassan outlook.com> writes:
anyone? so there is no way to get my 50,000 Line of code to work 
again? All that code to waste?

P.S: I've tried doing these:
Tried to use GDC, no luck
used -defaultlib=libphobos2.so, no luck
removed all functions from library, compiled it empty, yet, 
dlopen gives segFault.

AND:
This same code used to work on ubuntu 14.04 (i386), now I'm on 
xubuntu 16.04 (amd64).
Oct 10 2016
parent Nafees <nafees.hassan outlook.com> writes:
Fixed this issue, stackoverflow helped 
http://stackoverflow.com/questions/39929495

just compiled the library with -fPIC -m32 -shared
Oct 10 2016