So it resolved by adding -cclib -lbsd after installed it the package libbsd-dev on Ubuntu. For info, the complete command is:

ocamlfind ocamlopt  \
  -linkpkg -package ctypes.foreign \
  -ccopt -I/usr/local/lib \
  -cclib "-Wl,--whole-archive" \
  -cclib -lmlx  \
  -cclib -lbsd \
  -cclib "-Wl,--no-whole-archive" \
  -cclib "-Wl,-E" \
  -cclib -lX11 \
  -cclib -lXext \
  minilibx.cmx \
  hello.ml \
  -o hello.native

On 07/31/2016 03:38 PM, Danny Willems wrote:
I found this post on GitHub: https://github.com/ocamllabs/ocaml-ctypes/issues/236
I added the arguments to pass to ld and the mlx_init error disappear (now I've an error about strlcpy used by minilibx which is not defined on Linux).

So my command is
```
ocamlfind ocamlopt  \
  -linkpkg -package ctypes.foreign \
  -ccopt -I/usr/local/lib \
  -cclib "-Wl,--whole-archive" \
  -cclib -lmlx  \
  -cclib "-Wl,--no-whole-archive" \
  -cclib "-Wl,-E" \
  -cclib -lX11 \
  -cclib -lXext \
  minilibx.cmx \
  hello.ml \
  -o hello.native
```

On 07/31/2016 03:27 PM, Matthew Saffer wrote:

If ld can't find the library, don't you need to change your LD_LOAD_PATH?

On Sun, Jul 31, 2016, 06:35 Danny Willems <contact@danny-willems.be> wrote:
If I use a relative path (for example -cclib -L../minilibx) with
ocamlopt, it compiles but I have the same issue when running the binary.

On 07/31/2016 12:22 PM, Danny Willems wrote:
> 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.
>
>


--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs