Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how to load/link libraries in the tutorial #73

Open
yallop opened this issue Sep 2, 2013 · 5 comments
Open

Explain how to load/link libraries in the tutorial #73

yallop opened this issue Sep 2, 2013 · 5 comments

Comments

@yallop
Copy link
Owner

yallop commented Sep 2, 2013

This comes up rather often (e.g. #41, #49, #70, #71), and the current tutorial doesn't cover it at all.

Chapter 19 of Real World OCaml has some helpful remarks.

@yawnt
Copy link

yawnt commented Feb 17, 2016

👍 , I was having trouble loading a static library on ArchLinux. Converting the very same .a to a .so fixed it. I'd be curious as to how to link against static libraries (-force_load doesn't seem to exist, I'm using Oasis to compile, if that helps)

Thanks for the great work :)

@XVilka
Copy link

XVilka commented Dec 1, 2017

I would like to know how to link with static libraries as well.

@Ilazki
Copy link

Ilazki commented Jan 23, 2020

This issue is still relevant; I just spent far too long trying to figure out how to load and link in a library to use with ctypes.foreign, largely because the tutorial only uses toy examples that interface with already-loaded libraries via the toplevel.

I eventually found what I needed to use dune to include a C library, thanks to a single, unexplained line of code in Real World OCaml's FFI section (using the flags stanza to pass -cclib -lname to ocamlc/ocamlopt), but it was pretty inconvenient getting there, with far too long wasted trying to find a way to load .so files at the toplevel because of the tutorial's use of only toplevel-capable toy examples. At this point I'm still not sure if there's a way to load C libraries into a toplevel directly for experimentation or not, but it's not a big deal since I have what I need for the moment.

This is a cool project that seems to really improve the convenience of making library bindings, but the documentation is still severely lacking despite there being a 6+ year old bug report on the documentation shortcomings. :(

@yallop
Copy link
Owner Author

yallop commented Jan 23, 2020

@Ilazki:

At this point I'm still not sure if there's a way to load C libraries into a toplevel directly for experimentation or not, but it's not a big deal since I have what I need for the moment.

Is the following the sort of thing you're looking for?

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
[...]
- : unit = ()
# #require "ctypes.foreign";;
[...]
/home/jeremy/.opam/4.09.0/lib/ctypes/ctypes-foreign-unthreaded.cma: loaded
# let libclingo = Dl.dlopen ~filename:"/usr/lib/libclingo.so" ~flags:Dl.[RTLD_NOW];;
val libclingo : Dl.library = <abstr>
# let clingo_version = Foreign.foreign ~from:libclingo "clingo_version"
      Ctypes.(ptr int @-> ptr int @-> ptr int @-> returning void);;
val clingo_version :
  int Ctypes_static.ptr ->
  int Ctypes_static.ptr -> int Ctypes_static.ptr -> unit = <fun>
# let major, minor, revision = Ctypes.(allocate int 0, allocate int 0, allocate int 0);;
val major : int Ctypes.ptr = (int*) 0x55a608bbff00
val minor : int Ctypes.ptr = (int*) 0x55a608bbfee0
val revision : int Ctypes.ptr = (int*) 0x55a608bbfec0
# clingo_version major minor revision;;
- : unit = ()
# Ctypes.(Printf.printf "Clingo version: %d.%d.%d\n" !@major !@minor !@revision);;
Clingo version: 5.3.0
- : unit = ()

@Ilazki
Copy link

Ilazki commented Jan 23, 2020

Yes, that seems to be precisely what I was originally after, thank you very much. I like to experiment in a REPL first, sort of work out a rough draft of what I'm after in it, but after failing to find any information on doing something like that, I was going to just give up on the idea and work with a save/compile/test cycle using what info could find.

I didn't even expect an answer, I just wrote my comment to let you know there are still new people finding this library, thinking it's pretty awesome, but getting confounded by the lack of info online. :)

Anyhow, thanks again, that's pretty convenient and will make things a lot easier for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants