www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to a link to a C library to create static bindings?

reply thebluepandabear <therealbluepandabear protonmail.com> writes:
Hello,

I am wanting to create my own C bindings to CSFML, and I have 
been extremely struggling with this.

There is no resources or tutorials for how to do this so I am 
basically on my own.

First thing I looked at was `dstep` which helps translate C 
header files to D language, so I ran `dstep` and translated the C 
header files to D language. This went well. I ended up wrapping 
all the code in an `extern(C)` call.

Now, I am trying to 'link' CSFML to my bindings but have been 
struggling to do so.

I tried to add the following to `dub.json`:

```
"libs": ["csfml-audio", "csfml-graphics"]
```

But it came back with an error:

```
/usr/bin/ld: 
/home/tom/Desktop/sfmld-backend/out/production/sfmld-backend/
bj/sfmld-backend.o: in function `_Dmain':
/home/tom/Desktop/sfmld-backend/source/graphics.d:(.text._Dmain[_Dmain]+0x3a):
undefined reference to `sfRenderWindow_create'
/usr/bin/ld: 
/home/tom/Desktop/sfmld-backend/source/graphics.d:(.text._Dmain[_Dmain]+0x58):
undefined reference to `sfRenderWindow_isOpen'
/usr/bin/ld: 
/home/tom/Desktop/sfmld-backend/source/graphics.d:(.text._Dmain[_Dmain]+0x69):
undefined reference to `sfRenderWindow_pollEvent'
/usr/bin/ld: 
/home/tom/Desktop/sfmld-backend/source/graphics.d:(.text._Dmain[_Dmain]+0x7c):
undefined reference to `sfRenderWindow_close'
/usr/bin/ld: 
/home/tom/Desktop/sfmld-backend/source/graphics.d:(.text._Dmain[_Dmain]+0x90):
undefined reference to `sfRenderWindow_clear'
/usr/bin/ld: 
/home/tom/Desktop/sfmld-backend/source/graphics.d:(.text._Dmain[_Dmain]+0x99):
undefined reference to `sfRenderWindow_display'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
```

I am really confused as to how I even am supposed to get the 
library name in the first place, which is another thing that is 
confusing me.

Any help with this would be appreciated.
Jan 27 2023
next sibling parent thebluepandabear <therealbluepandabear protonmail.com> writes:
When I do run with dub I just get:

```
Program exited with code -11

```
Jan 27 2023
prev sibling parent evilrat <evilrat666 gmail.com> writes:
On Saturday, 28 January 2023 at 02:40:58 UTC, thebluepandabear 
wrote:
 I am really confused as to how I even am supposed to get the 
 library name in the first place, which is another thing that is 
 confusing me.
It is up to the library author to choose a name. The extensions is `.so/.dll/.dylib` for shared libraries on nix/Windows/Mac and `.a/.lib/???` for static libraries. The second part, this looks like sfml graphics depends on sfml render or window or something like that, you'd better check `readelf -d sfml-render.so | grep 'NEEDED'` if you have dynamic libs to get a clue what it might need, or go search library that contains missing symbols by hand.
Jan 27 2023