Ctypes and toplevel error

Hi,

I’m struggling with trying to make C-bindings available to the toplevel, using the ctypes library. I tried to replicate the example from this thread: Bytecode compilation, correct invocation · Issue #304 · yallop/ocaml-ctypes · GitHub
but it fails as follows:

$ ocaml
        OCaml version 4.13.0

Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

Down v0.0.4 loaded. Type Down.help () for more info.
# #thread;;
/home/san/.opam/4.13.0/lib/ocaml/threads: added to search path
/home/san/.opam/4.13.0/lib/ocaml/unix.cma: loaded
/home/san/.opam/4.13.0/lib/ocaml/threads/threads.cma: loaded
# #require "mkrepo";;
/home/san/.opam/4.13.0/lib/bigarray-compat: added to search path
/home/san/.opam/4.13.0/lib/bigarray-compat/bigarray_compat.cma: loaded
/home/san/.opam/4.13.0/lib/bytes: added to search path
/home/san/.opam/4.13.0/lib/stdlib-shims: added to search path
/home/san/.opam/4.13.0/lib/stdlib-shims/stdlib_shims.cma: loaded
/home/san/.opam/4.13.0/lib/integers: added to search path
/home/san/.opam/4.13.0/lib/integers/integers.cma: loaded
/home/san/.opam/4.13.0/lib/ctypes: added to search path
/home/san/.opam/4.13.0/lib/ctypes/ctypes.cma: loaded
/home/san/.opam/4.13.0/lib/ctypes/ctypes-top.cma: loaded
/home/san/.opam/4.13.0/lib/ctypes/ctypes-foreign.cma: loaded
/home/san/.opam/4.13.0/lib/mkrepo: added to search path
/home/san/.opam/4.13.0/lib/mkrepo/mkrepo.cma: loaded
Exception:
Dl.DL_error
 "/home/san/.opam/4.13.0/lib/stublibs/dllctypes-foreign_stubs.so: undefined symbol: git_repository_init".

I suspect that example is too old now. What is the new way of doing such things?
Subsidiary question: can I use dune for this?

You may be able to resolve this by passing -no-as-needed to the linker, to indicate that it should not drop symbols that may only be required dynamically. More specifically, you can change the call to ocamlmklib

ocamlfind ocamlmklib -linkall -package ctypes.foreign -lgit2 mkrepo.cmo empty.o -o mkrepo -oc mkrepo

to the following:

ocamlfind ocamlmklib -linkall -package ctypes.foreign -ldopt -Wl,-no-as-needed -lgit2 mkrepo.cmo empty.o -o mkrepo -oc mkrepo
1 Like

Thanks, yes this works for linux, but IIUC this would not be compatible with mac os