caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] OCaml-ctypes compilation with library in a different directory
@ 2016-07-31 10:22 Danny Willems
  2016-07-31 10:34 ` Danny Willems
  0 siblings, 1 reply; 4+ messages in thread
From: Danny Willems @ 2016-07-31 10:22 UTC (permalink / raw)
  To: caml-list

Hello,

I'm writing a binding OCaml to the C library "minilibx", here a mirror 
of the library https://github.com/dannywillems/minilibx.

The Makefile installs the library file libmlx.a in /usr/local/lib and 
you have to add the flags -lmlx -lX11 and -lXext to compile. So you have 
something like that to compile a test file written in C:

```
gcc  -I /usr/local/include -L/usr/local/lib test.c -lmlx  -lX11 -lXext
```

I wrote a simple binding to mlx_init, mlx_new_window and mlx_loop in 
minilibx.ml:

```
open Ctypes
open Foreign

type mlx_ptr = unit ptr
type mlx_win = unit ptr

let mlx_ptr : mlx_ptr typ = ptr void
let mlx_win : mlx_win typ = ptr void

let mlx_init =
   foreign "mlx_init" (void @-> returning mlx_ptr)

let mlx_new_window =
   foreign "mlx_new_window" (mlx_ptr @-> int @-> int @-> string @-> 
returning mlx_win)

let mlx_loop =
   foreign "mlx_loop" (mlx_ptr @-> returning void)
```
with the corresponding interface in minilibx.mli and compile it with

```
ocamlfind ocamlc -c -package ctypes.foreign -linkpkg minilibx.mli

ocamlfind ocamlc -c -package ctypes.foreign -linkpkg minilibx.ml
```

Now I wrote a test file named hello.ml
```
let () =
   let i = Minilibx.mlx_init () in
   let w = Minilibx.mlx_new_window i 640 480 "Hello, World" in
   Minilibx.mlx_loop i
```

and compile it with

```
ocamlfind ocamlc -o hello -ccopt -L/usr/local/lib -cclib -lmlx -cclib 
-lX11 -cclib -lXext minilibx.cmo -package ctypes.foreign -linkpkg hello.ml
```
and when I execute hello, I have

```
Fatal error: exception 
Dl.DL_error("/home/dannywillems/.opam/ctypes/lib/stublibs/dllctypes-foreign-base_stubs.so: 
undefined symbol: mlx_init")
```

So linking seems not working. But I can't find why.

If I try to compile in native with ocamlopt (cmo -> cmx), linking is not 
done at compile time:
```
/usr/bin/ld: cannot find -lmlx
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
```

Am I using the right option (-ccopt -L/usr/local/lib)? And why ocamlc 
doesn't output the error linking message?

Thank you for you help.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-07-31 13:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-31 10:22 [Caml-list] OCaml-ctypes compilation with library in a different directory Danny Willems
2016-07-31 10:34 ` Danny Willems
     [not found]   ` <CAAefXT_Q217YGCR9=kmiJWT5U47TfYKvt8TD5B0yo+binE52Cg@mail.gmail.com>
2016-07-31 13:38     ` Danny Willems
2016-07-31 13:48       ` Danny Willems

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).