ocamlutopocaml-dune

Dune behaviour regarding .cma files


I am experiencing some issues when trying to create a .cma file (library) with the dune utility. I have the following project tree:

.
├── _build
│   ├── default
│   │   ├── dune
│   │   └── lib
│   │       ├── a.a
│   │       ├── a.cma
│   │       ├── a.cmxa
│   │       ├── a.cmxs
│   │       ├── a.ml
│   │       └── dune
│   └── log
├── dune
├── dune-project
└── lib
    ├── a.ml
    └── dune

Where a.ml declares a very simple function f, as an example. When I run directly ocamlc -o a.cma -a a.ml, and then fire up utop a.cma, I am able to perform, as desired, the following command:

utop # A.f;;
- : int -> int = <fun>

On the other hand, after one use of dune build (which generates the _build directory and its sub-directories), the _build/default/lib/a.cma file that I get takes ten times as much space as the manually-generated one, and more importantly, does not work. Both commands utop _build/default/lib/a.cma and cd _build/default/lib; utop a.cma are unsuccesful and I am unable to use the module A:

utop # A.f;;
Line 1, characters 0-3:
Error: Unbound module A

I know about the dune utop command, but what if I want to export/share my library elsewhere, where dune is not installed? What am I supposed to do? Am I using dune the wrong way? Thank you in advance


PS: File content:

lib/dune

(library
 (name a))

Solution

  • You need to make visible the bytecode object files in .a.objs/byte. For instance with:

    #directory ".a.objs/byte"
    

    (Those files are installed as usual for library users)

    Also note that dune is by default in wrapped mode: the module M from library Lib is accessible as Lib.M, except if the user defines an entry point module with the the same name Lib as the whole library.