Thread overview
How do I load a shared library?
Oct 08, 2016
Nafees
Oct 08, 2016
Adam D. Ruppe
Oct 08, 2016
Nafees
Oct 10, 2016
Nafees
Oct 10, 2016
Nafees
October 08, 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.
October 08, 2016
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?
October 08, 2016
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
October 10, 2016
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).
October 10, 2016
Fixed this issue, stackoverflow helped http://stackoverflow.com/questions/39929495

just compiled the library with -fPIC -m32 -shared