www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem with shared library and dlopen() function

reply "=?ISO-8859-2?Q?Micha=B3_Cie=B6lak?=" <admin.michalc gmail.com> writes:
Content-Disposition: inline

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.
Aug 22 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Aug 24 2007
parent reply Witold Baryluk <baryluk smp.if.uj.edu.pl> writes:
Dnia Fri, 24 Aug 2007 17:08:54 +1000
Daniel Keep <daniel.keep.lists gmail.com> napisa=B3/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. --=20 Witold Baryluk, aleph0
Aug 24 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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.
Aug 25 2007