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

* Re: [Caml-list] OCaml-ctypes compilation with library in a different directory
  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>
  0 siblings, 1 reply; 4+ messages in thread
From: Danny Willems @ 2016-07-31 10:34 UTC (permalink / raw)
  To: caml-list

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.
>
>


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

* Re: [Caml-list] OCaml-ctypes compilation with library in a different directory
       [not found]   ` <CAAefXT_Q217YGCR9=kmiJWT5U47TfYKvt8TD5B0yo+binE52Cg@mail.gmail.com>
@ 2016-07-31 13:38     ` Danny Willems
  2016-07-31 13:48       ` Danny Willems
  0 siblings, 1 reply; 4+ messages in thread
From: Danny Willems @ 2016-07-31 13:38 UTC (permalink / raw)
  To: Matthew Saffer, caml-list

[-- Attachment #1: Type: text/plain, Size: 4101 bytes --]

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 
> <mailto: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 <http://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
>     <http://minilibx.ml>
>     > ```
>     >
>     > Now I wrote a test file named hello.ml <http://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 <http://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
>


[-- Attachment #2: Type: text/html, Size: 7426 bytes --]

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

* Re: [Caml-list] OCaml-ctypes compilation with library in a different directory
  2016-07-31 13:38     ` Danny Willems
@ 2016-07-31 13:48       ` Danny Willems
  0 siblings, 0 replies; 4+ messages in thread
From: Danny Willems @ 2016-07-31 13:48 UTC (permalink / raw)
  To: caml-list, Matthew Saffer

[-- Attachment #1: Type: text/plain, Size: 4751 bytes --]

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 
>> <mailto: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 <http://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 <http://minilibx.ml>
>>     > ```
>>     >
>>     > Now I wrote a test file named hello.ml <http://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 <http://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
>>
>


[-- Attachment #2: Type: text/html, Size: 8713 bytes --]

^ 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).