Dynamically calling external libraries.

Marc Schütz" <schuetzm at gmx.net> Marc Schütz" <schuetzm at gmx.net>
Fri Feb 28 10:11:20 PST 2014


On Friday, 28 February 2014 at 18:05:13 UTC, Marc Schütz wrote:
> On Friday, 28 February 2014 at 17:29:09 UTC, Setra wrote:
>> I am using  dmd_2.065.0-0_i386.deb on a 32 bit machine.
>
> Ah, I can reproduce it with 32bit executables:
>
> # dmd -m32 main1.d -L-ldl
> # dmd -m32 -c dll.d -fPIC
> # dmd -m32 -oflibdll.so dll.o -shared -defaultlib=
> # LD_LIBRARY_PATH=. ./main1
> +main()
> libdll.so is loaded
> dll() function is found
> dll()
> 134684056
> unloading libdll.so
> -main()
> # file ./main1
> ./main1: ELF 32-bit LSB executable, Intel 80386, version 1 
> (SYSV), dynamically linked (uses shared libs), for GNU/Linux 
> 2.6.16, BuildID[sha1]=a0eb9b8af3a95d0c4fddd2daf7a476aabed4b53e, 
> not stripped
>
> Might be a compiler bug...

No, it isn't. The problem is here:
int function(int x) fn = cast(int function(int x))dlsym(lh, 
"dll");

This should be:
extern(C) int function(int x) fn = cast(int function(int 
x))dlsym(lh, "dll");

The calling conventions for C and D are different on x86, but 
happen to agree on x86_64 (at least the parts that are relevant 
here).

Btw, the forward declaration on top "extern (C) int dll();" isn't 
used anywhere.


More information about the Digitalmars-d-learn mailing list