I have a file Tools.ml which contains some common utility functions I write myself. Under .../Code/ I have several folders which each contains a project. My question is where I should place this Tools.ml such that all the folders and files under .../Code/ could share this module by Open Tools.
Hope my question is clear… Does anyone have a good solution?
Edit1: Following @gasche’s answer, I have written tools.ml as follows:
module Tools =
struct
let a_function = ...
...
end
Then I compiled it, and done ocamlfind install tools META tools.cmo tools.cmx tools.ml as suggested, which looks going well. Then I have written test.ml as follows:
open Tools
let f = Tools.a_function
then I compiled it with ocamlc test.ml -o test, then I got an error:
File "test.ml", line 1, characters 0-1:
Error: Error while linking test.cmo:
Reference to undefined global `Tools'
Could anyone tell me what happened?
You could package it as an independent library, install it with other OCaml libraries, and access to it, from your project, as a library.
A very simple way to do this is to write a META file for
ocamlfind. Create a directory somewhere you’re comfortable to hold you “personal library” project. Suppose you havetools.mlandtools.mli, and your code depends on some findlib package (eg.unixandbigarray). You META would look like this:Once you have written this META file, it is easy to ask
ocamlfindto “install” the library (and remove it if you want to), and use it in your other projects. To install, the syntax isocamlfind install <name> <meta-file> <file1> <file2> ...where<file1>, <file2>..are the file you wish to see included in the installation directory. You must at least havetools.cmitools.cmo(andtools.oandtools.cmxfor native compilation), but it is good practice to also havetools.mlifor example (and, if you want to provide the code,tools.ml).(Of course
tools.cmoetc. have to exist, that is you mustinstallafter you have compiled your package. If you have usedocamlbuild, they are likely to be in a_buildsubdirectory, soocamlfind install ... _build/tools.cmo ....)From your numerous projects, you can use your library easily, either using the ocamlfind toold directly if this is what you already do to compile your programs
or through the facilities provided by
ocamlbuildfor example, addingpackage(tools)to your tags.To reinstall your library if you made a change to it and want it accessible from your projects
You could also handle all this through
oasis, which is a layer on top of ocamlfind/ocamlbuild to automate this process. I’m not familiar enough withoasisto give such examples off the top of my head, but it should be equally simple for such a restricted case (one-file library), and scale better if you wish later to extend your library (eg. it can also handle documentation generation, pre-compilation configuration…).