Fmt module usage

I’m trying to learn ocaml and wanted to see weather I could get printing of data structures working and run into a problem:
I saw that there is a module called Fmt (https://github.com/dbuenzli/fmt) and installed it via “opam install fmt”. Inside “ocaml” I can do a “#require “fmt”” . However I cannot use the library with “ocamlc” and “open Fmt;;” . Is the Fmt module only used for the interactive ocaml ?

You should be able to compile you source with:

ocamlfind ocamlc -c -package fmt  mysource.ml

If you want to compile things directly with ocamlc and use libraries installed via opam, see this tutorial.

(Also you should not open Fmt it’s bad style, use qualified names e.g. Fmt.string).

1 Like
> a.ml echo 'open Fmt'
ocamlc -c a.ml

This fails with

Error: Unbound module Fmt

This works

ocamlc -I $(opam var lib)/fmt -c a.ml

I suggest you invest some time in learning the basics of dune. But, I guess it depends on what works for you

I’m using (m.ml):

let () =
  let str u = Format.asprintf "%a" Fmt.Dump.uchar u in
  Printf.printf "%s\n" (str Uchar.min)
;;

I want to compile it to a binary that I can execute. How is the command to achieve this? The above command uses -c, so I guess it only compiles an object file. However how can I then link it? I thought I could use ocamlfind ocamlc -package fmt m.ml and get a.out but it just returns silently.

I’m wondering about the “silently” you should get a link error but this should do it:

> ocamlfind ocamlc -linkpkg -package fmt m.ml
> ./a.out
U+0000

That made it work : -linkpkg Thanks. I have another question about howto use Fmt to print i.e. Lists but I open open thread.

For each module installed with opam a directory is created under $(opam var lib).
Is there no easier way instead to provide all the install directories for each module to be included?

Don’t think so. You can put all in a big shared directory, but how will you defend against Library A overwriting files of Library B?

And that’s why people use build systems.

1 Like