Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Danny Willems <contact@danny-willems.be>
To: caml-list@inria.fr
Subject: [Caml-list] OCaml-ctypes compilation with library in a different directory
Date: Sun, 31 Jul 2016 12:22:20 +0200	[thread overview]
Message-ID: <b9bde195-ccb3-9371-c423-a7ad39ab7cdb@danny-willems.be> (raw)

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.


             reply	other threads:[~2016-07-31 10:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-31 10:22 Danny Willems [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b9bde195-ccb3-9371-c423-a7ad39ab7cdb@danny-willems.be \
    --to=contact@danny-willems.be \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox