www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - Bind D as a shared lib for ldc

reply Prasun Anand <prasunanand.bitsp gmail.com> writes:
Hi,

I would like to bind D as a shared library so that I can call it 
from
Ruby using FFI .

I found out how to do it with DMD from this 
[post](https://wiki.dlang.org/Call_D_from_Ruby_using_FFI).

But how should I so I do it with LDC?

I tried the example from above post and get the following error:

```
$ ldc2 -shared -m64 -relocation-model=pic i.d
/usr/bin/ld: 
/home/prasun/ldclatest/ldc-1.1.0-pk9rkm4zvdp6pglam7s2/lib/libdrunti
e-ldc.a(errno.c.o): relocation R_X86_64_PC32 against undefined symbol
`__errno_location  GLIBC_2.2.5' can not be used when making a shared object;
recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Error: /usr/lib/nvidia-cuda-toolkit/bin/gcc failed with status: 1
```

Regards,
Prasun
Jul 25 2017
next sibling parent Prasun Anand <prasunanand.bitsp gmail.com> writes:
$ ldc2 -version
LDC - the LLVM D compiler (1.1.0):
   based on DMD v2.071.2 and LLVM 3.7.1
   built with LDC - the LLVM D compiler (0.17.1)
   Default target: x86_64-unknown-linux-gnu
   Host CPU: bdver2
   http://dlang.org - http://wiki.dlang.org/LDC

   Registered Targets:
     aarch64    - AArch64 (little endian)
     aarch64_be - AArch64 (big endian)
     amdgcn     - AMD GCN GPUs
     arm        - ARM
     arm64      - ARM64 (little endian)
     armeb      - ARM (big endian)
     bpf        - BPF (host endian)
     bpfeb      - BPF (big endian)
     bpfel      - BPF (little endian)
     cpp        - C++ backend
     hexagon    - Hexagon
     mips       - Mips
     mips64     - Mips64 [experimental]
     mips64el   - Mips64el [experimental]
     mipsel     - Mipsel
     msp430     - MSP430 [experimental]
     nvptx      - NVIDIA PTX 32-bit
     nvptx64    - NVIDIA PTX 64-bit
     ppc32      - PowerPC 32
     ppc64      - PowerPC 64
     ppc64le    - PowerPC 64 LE
     r600       - AMD GPUs HD2XXX-HD6XXX
     sparc      - Sparc
     sparcel    - Sparc LE
     sparcv9    - Sparc V9
     systemz    - SystemZ
     thumb      - Thumb
     thumbeb    - Thumb (big endian)
     x86        - 32-bit X86: Pentium-Pro and above
     x86-64     - 64-bit X86: EM64T and AMD64
     xcore      - XCore

Regards,
Prasun
Jul 25 2017
prev sibling next sibling parent kinke <kinke gmx.net> writes:
On Wednesday, 26 July 2017 at 06:53:36 UTC, Prasun Anand wrote:
 $ ldc2 -shared -m64 -relocation-model=pic i.d
 /usr/bin/ld: 
 /home/prasun/ldclatest/ldc-1.1.0-pk9rkm4zvdp6pglam7s2/lib/libdrunti
e-ldc.a(errno.c.o): relocation R_X86_64_PC32 against undefined symbol
`__errno_location  GLIBC_2.2.5' can not be used when making a shared object;
recompile with -fPIC
The druntime and Phobos libraries you're linking against are static ones and haven't been compiled as position-independent-code. The LDC 1.3 package ships with shared runtime libs too; select them via `-defaultlib=druntime-ldc-shared,phobos2-ldc-shared -debuglib=druntime-ldc-debug-shared,phobos2-ldc-debug-shared`, that should work.
Jul 26 2017
prev sibling parent reply David Nadlinger via digitalmars-d-ldc <digitalmars-d-ldc puremagic.com> writes:
Hi Prasun,

On 26 Jul 2017, at 7:53, Prasun Anand via digitalmars-d-ldc wrote:
 But how should I so I do it with LDC?

 I tried the example from above post and get the following error:

 ```
 $ ldc2 -shared -m64 -relocation-model=pic i.d
 /usr/bin/ld: 
 /home/prasun/ldclatest/ldc-1.1.0-pk9rkm4zvdp6pglam7s2/lib/libdrunti
e-ldc.a(errno.c.o): 
 relocation R_X86_64_PC32 against undefined symbol 
 `__errno_location  GLIBC_2.2.5' can not be used when making a shared 
 object; recompile with -fPIC
 /usr/bin/ld: final link failed: Bad value
 collect2: error: ld returned 1 exit status
 Error: /usr/lib/nvidia-cuda-toolkit/bin/gcc failed with status: 1
 ```
You need to use a version of LDC built with shared druntime/Phobos – the static ones (libdruntime-ldc.a in the linker error message) are not compatible with creating shared libraries. — David
Jul 26 2017
parent Prasun Anand <prasunanand.bitsp gmail.com> writes:
Thanks Kinke and David :)

Regards,
Prasun
Jul 28 2017