Quantcast
Channel: OCaml - Topics tagged dune
Viewing all articles
Browse latest Browse all 540

How to properly define a library with C bindings?

$
0
0

I’m using ctypes and writing custom very small bindings to libsecp256k1, and I can get it to work, but only if I add (flags :standard -cclib -lsecp256k1) on my executable.

But that feels wrong, I think it should be able to work by just declaring these things on the library side. Here’s everything I have (minimal reproducible example):

The same code is here: https://github.com/andunieee/dune-ffi-repro

If you run it with dune exec ./bin/main.exe it will give this message:

Fatal error: exception Dl.DL_error("_build/default/bin/main.exe: undefined symbol: secp256k1_context_create")

Also calling dune build ./bin/main.exe and then ldd ./_build/default/bin/main.exe will give

linux-vdso.so.1 (0x00007ffdaa7f5000)
libffi.so.8 => /usr/lib/libffi.so.8 (0x00007b726ea12000)
libzstd.so.1 => /usr/lib/libzstd.so.1 (0x00007b726e72d000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007b726e640000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007b726e45e000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007b726ea4b000)

Which means the reference to libsecp256k1 is not being acknowledged somewhere in the compilation or linking step (I don’t understand these things).

But if you edit bin/dune with this:

 (executable
  (name main)
+ (flags :standard -cclib -lsecp256k1)
  (libraries dune_ffi_repro))

then everything will work (well, you must have libsecp256k1 installed locally).


What am I doing wrong?

6 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 540