Thread overview
problem with shared library and dlopen() function
Aug 22, 2007
Michał Cieślak
Aug 24, 2007
Daniel Keep
Aug 25, 2007
Witold Baryluk
Aug 25, 2007
Frits van Bommel
August 22, 2007
At first I want to apologize for my English. I want to rewrite my Java program to D. I have problem with shared library and dlopen() function.

calc_mean.d:

extern(C){
     double mean(double a, double b) {
             return (a+b) / 2;
     }
}

dmd -fPIC calc_mean.d -c
gcc -shared -Wl,-soname,libmean.so.1 -o libmean.so.1.0.1  calc_mean.o -m32


test.d:

import std.stdio;
import std.c.linux.linux ;

public void main(){
     void *handle;
     double (*reff)(double, double);

     handle = dlopen("./libmean.so.1.0.1", RTLD_NOW);

     char *errstr = dlerror();

     if(errstr != null)
          printf ("A dynamic linking error occurred: (%s)\n", errstr);

     *cast(void **)(&reff) = dlsym(handle, "mean");
     errstr = dlerror();

     if(errstr != null)
          printf ("A dynamic linking error occurred: (%s)\n", errstr);

     writefln((*reff)(1,12));
}



dmd test -L-ldl

./test :

./test
A dynamic linking error occurred: (./libmean.so.1.0.1: undefined symbol:
_Dmodule_ref)
A dynamic linking error occurred: (./test: undefined symbol: mean)
Segmentation fault

I am 17, so I am novice programmer. I don't know what I am doing wrong. Please help me.


August 24, 2007

Michał Cieślak wrote:
> At first I want to apologize for my English. I want to rewrite my Java program to D. I have problem with shared library and dlopen() function.
> 
> [snip]

Last time I checked, DMD couldn't generate position-independent code on linux, which means it can't compile shared libraries.

You might have more luck with the GDC compiler, but since I don't use linux, that's just a guess.

You might also want to have a look at DDL <http://www.dsource.org/projects/ddl/>.

	-- Daniel
August 25, 2007
Dnia Fri, 24 Aug 2007 17:08:54 +1000
Daniel Keep <daniel.keep.lists@gmail.com> napisał/a:


> Last time I checked, DMD couldn't generate position-independent code on linux, which means it can't compile shared libraries.

True. I was hardly trying but DMD can't do this.

> You might have more luck with the GDC compiler, but since I don't use linux, that's just a guess.

GDC is also for Win32.

-- 
Witold Baryluk, aleph0
August 25, 2007
Witold Baryluk wrote:
> Dnia Fri, 24 Aug 2007 17:08:54 +1000
> Daniel Keep <daniel.keep.lists@gmail.com> napisał/a:
> 
> 
>> Last time I checked, DMD couldn't generate position-independent code
>> on linux, which means it can't compile shared libraries.
> 
> True. I was hardly trying but DMD can't do this.
> 
>> You might have more luck with the GDC compiler, but since I don't use
>> linux, that's just a guess.
> 
> GDC is also for Win32.

I think his point is that the issue is on Linux. There's a difference between Linux .so libraries and Windows DLLs: the latter don't require position-independent code (IIRC they contain relocation info) and DMD/Windows can in fact create them.