Is there a reason to save .cmo or just .cma?

Let's say I have mylibrary.ml that provides wrappings for library.c , and I want to compile bytecode and provide mylibrary.ml as a library for other mylibrary.ml code. Compiling this into bytecode (and I am not considering compiling ocaml to my own code here) produces several files, and I wonder if there is any reason to save them? Or provide them to all other library users?

I (so far) understand that I need the mylibrary.cma bytecode library object so that I can use mylibrary in ocaml toplevel as

 ocaml mylibrary.cma 

or what can i

 #load "mylibrary.cma";; 

from ocaml script. Then there should also be a compiled interface mylibrary.cmi and dllmylibrary.so (which contains the C-parts of the code) needed to work above. And the description file for the mylibrary.mli interface mylibrary.mli is good for storing documentation.

But is there any reason to save mylibrary.cmo if I have mylibrary.cma ? In which case would someone like it too?

EDIT: I want to say that I need to build .cmo in a makefile and then use it to build .cma , but after that I decided to remove .cmo to keep the directory slightly clean.

+4
source share
2 answers

Thus, it is obvious that the purpose of different files is (by limiting this to the bytecode compiler):

mylibrary.mli - definition of human readable interface (not required, compiler needs only .cmi)
mylibrary.cmi - the compiled interface needed when compiling the code that calls mylibrary
library.o - object-object C

dlllibrary.so - a shared library object made from .o
dlllibrary.a - a static library object made from .o
mylibrary.cmo - a bytecode object compiled from mylibrary.ml
mylibrary.cma - bytecode library

Then mylibrary.cma (with mylibrary.cmi and dlllibrary.so ) are needed when loading mylibrary from the top level:

#load "mylibrary.cma";;

OR

You can compile a bytecode program that dynamically links to mylibrary.cma ( mylibrary.cmi and dlllibrary.so also needed):

ocamlc mylibrary.cma <program>.ml

OR

dynamic binding to a bytecode object instead of a bytecode library (required files: mylibrary.cmo , mylibrary.cmi , dlllibrary.so ):

ocamlc dlllibrary.so mylibrary.cmo <program>.ml

(Note: then run the bytecode with: ocamlrun -I . <program> , assuming dlllibrary.so is in the current directory.)

OR

static binding to objects (required files: mylibrary.cmo , mylibrary.cmi liblibrary.a )

ocamlc -custom liblibrary.a mylibrary.cmo <program>.ml

OR

static binding to library objects (required files: mylibrary.cma , mylibrary.cmi , liblibrary.a )

ocamlc -custom -I . mylibrary.cma <program>.ml

Thus, depending on how the library will be used in the future, different files are needed. With the exception of .mli it is required only for readers, and the .o object file compiled from the C library is not needed (but in some cases it will be required when compiling into native code).

+3
source

You must also keep the source code of the library. Very often, when updating the Ocaml compiler (for example, from 3.12 to the future 3.13), the previous *.cma or *.cma will not be able to work without recompilation.

Assuming that you can always clean and recompile things (for example, you have two traditional goals, clean and all before make ), you can save *.cma

+2
source

Source: https://habr.com/ru/post/1387950/


All Articles