# C Bindings help? Trying to port sunvox library

**URL:** <https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732>\
**Category:** Help & Support\
**Created:** [May 6, 2019, 11:44am UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732 "2019-05-06T11:44:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 6, 2019, 11:44am UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/1 "2019-05-06T11:44:40Z")

</div>

Hello all,

Been working on trying to get C Bindings for [sunvox](http://www.warmplace.ru/soft/sunvox/), and I’m looking for a little guidance on how to fix an issue.

I installed [sunvox.so](https://github.com/warmplace/sunvox_dll/tree/master/linux/lib_x86_64) into /usr/lib, and also installed the header, [sunvox.h](https://github.com/warmplace/sunvox_dll/blob/master/headers/sunvox.h), into /usr/include. I proved I can link it by compiling one of the sample programs included, I noticed it is both a C++ file and a C file, so I used gcc to force C compiliation using `gcc -x c sunvox.c -o sunvox -lm -ldl -lsunvox `. This works, and it plays the sounds and everything just fine.

I then ran the [crystal\_lib](https://github.com/crystal-lang/crystal_lib) tool on the header to generate a binding, [sunshine.cr](https://github.com/redcodefinal/sunshine/blob/master/src/sunshine.cr). When trying to `crystal run ./src/sunshine.cr` I get this:

```auto
ian@gaming-computer:~/Documents/crystal/sunshine$ crystal run ./src/sunshine.cr 
_main.o: In function `__crystal_main':
/home/ian/Documents/crystal/sunshine/src/sunshine.cr:146: undefined reference to `sv_load_dll'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/home/ian/.cache/crystal/crystal-run-sunshine.tmp' -rdynamic -lsunvox -lpcre /usr/bin/../lib/crystal/lib/libgc.a -lpthread /usr/sha
re/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/usr/bin/../lib/crystal/lib -L/usr/lib -L/usr/local/lib`
```

Why doesn’t crystal see sunvox.so? What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [May 6, 2019, 10:22pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/2 "2019-05-06T22:22:01Z")

</div>

I do not use this feature of crystal much so I imagine other people will be able to help more.

I noticed in your example you are using `gcc.` Have you tried compiling with `llvm`?  
I don’t think that should make that big of a difference but it might.

Also, As of 0.28.0 there is an enviroment varible you can set called `CRYSTAL_LIBRARY_PATH` that you can use to point to the `.so` files. Crystal will still try to find `.so` files but will use this env variable first. I would look into it if you are having trouble with crystal finding libraries.  
Here is a link to the [RFC](https://forum.crystal-lang.org/t/rfc-link-configuration/546)

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 7, 2019, 6:02am UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/3 "2019-05-07T06:02:05Z")

</div>

Thanks for the suggestion! I tried it and it worked. This is what I did to test it.

```auto
$ clang -emit-llvm -c sunvox.c -o sunvox.bc
$ clang -o sunvox sunvox.bc -lm -lsunvox -ldl
$ ./sunvox
Desired audio buffer size: 2048 frames
ALSA: pulse
ALSA HW Default rate: 44100
ALSA HW Rate: 44100 frames
ALSA HW Buffer size: 4096 frames
ALSA HW Period size: 227
ALSA HW Periods: 0
ALSA SW Avail min: 227
ALSA SW Start threshold: 1
ALSA SW Stop threshold: 4096
SunVox lib version: 1.9.4
Loading SunVox song from file...
Loaded.
Project name: Tiny Tune. by NightRadio 2008
Number of modules: 8
module 0: Output; x=739 y=479
module 1: Generator; x=168 y=170
module 2: Echo; x=433 y=370
module 3: Generator2; x=299 y=849
module 4: Generator3; x=157 y=626
module 5: Generator4; x=646 y=860
module 6: Kicker; x=530 y=180
module 7: Filter; x=478 y=735
^CSOUND: sundog_sound_deinit() begin
SOUND: sundog_sound_deinit() end
Max memory used: 1668623
ian@gaming-computer:~/Documents/crystal/sunshine/src$ 
```

---

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [May 7, 2019, 6:13pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/4 "2019-05-07T18:13:08Z")

</div>

This might be a problem with crystal\_lib. Have you tried using `Link` in a crystal src file.

Here is an example from the docs page.  
[https://crystal-lang.org/reference/syntax\_and\_semantics/c\_bindings/lib.html](https://crystal-lang.org/reference/syntax_and_semantics/c_bindings/lib.html)

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 7, 2019, 6:42pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/5 "2019-05-07T18:42:45Z")

</div>

> <https://github.com/redcodefinal/sunshine/blob/master/src/sunshine.cr>

I’m pretty sure I linked it correctly. I also tried `@[Link("sunvox")]` but that didn’t work either.

---

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [May 7, 2019, 6:45pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/6 "2019-05-07T18:45:32Z")

</div>

have you opened an issue on lib\_crystal?

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 7, 2019, 6:47pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/7 "2019-05-07T18:47:45Z")

</div>

I would but, I’m not sure it’s a lib\_crystal issue, the issue is with it not finding the function, sv\_load\_dll. Which crystal\_lib did define

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 7, 2019, 7:06pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/8 "2019-05-07T19:06:02Z")

</div>

[https://asciinema.org/a/GhzKzsx9VHNWtHSiiyEVDe6K8](https://asciinema.org/a/GhzKzsx9VHNWtHSiiyEVDe6K8)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 7, 2019, 7:20pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/9 "2019-05-07T19:20:49Z")

</div>

Is `sunvox.so` compiled for C or C++? If it’s for C++ I think it won’t work.

What you can do is do `@[Link(ldflags: "sunvox.o")]` if you can compile the library to an `.o` file (you can make that path relative to the file where the lib is by doing `@[Link(ldflags: "#{ __DIR__ }/sunvox.o")]`)

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 7, 2019, 11:46pm UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/10 "2019-05-07T23:46:51Z")

</div>

I think it’s compiles for both. When I use the `gcc` flag `-x c` to force compilation in C, it works fine. Is .o the only supported type? I don’t have access to the source code of the sunvox library, it’s closed source and all we have is the .so file. Is there a way to check to see if the so is C++ or C only? I would imagine it’s both, since the header is written in a way to denote both would work.

> <https://github.com/redcodefinal/sunshine/blob/master/src/sunvox.h#L84>

Although I am a noob when it comes to some of the C++/C stuff, not really my forte too much.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 8, 2019, 12:46am UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/11 "2019-05-08T00:46:20Z")

</div>

I don’t know how to do it, but maybe there’s a command or way to list all exported symbols in a. so file. Then you can see if the function/symbol you want to use is there. C++ uses strange names when it compiles names (this is called mangling) and that might be the cause Crystal can’t find the function you want.

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 8, 2019, 3:29am UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/12 "2019-05-08T03:29:21Z")

</div>

That’s a good idea, I’ll definitely look into a tool to do it. I did test compile using `-x c` which forces the compiler to use C, from my understanding.

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 9, 2019, 5:39am UTC](https://forum.crystal-lang.org/t/c-bindings-help-trying-to-port-sunvox-library/732/13 "2019-05-09T05:39:51Z")

</div>

Hey so I used radare2 to get a better idea of what’s going on, and I found out something interesting, the `sv_load_dll` function isn’t even defined in my symbols, depsite the fact that it’s defined in my header file. I tried another function and it did work, so I guess problem solved. There isn’t really much in the way of documentation on this thing, the only thing I have to go by is the comments in the .h file.

Upon closer inspection I noticed the `sv_load_dll` function has some `ifdef` for Windows, but I don’t see why the function wouldn’t be defined in the unix version.

```auto
int sv_load_dll( void )
{
#ifdef WIN
    return sv_load_dll2( TEXT(LIBNAME) );
#else
    return sv_load_dll2( LIBNAME );
#endif
    return -1111;
}
```
