If I'm programming in the OCaml toploop and I want to use a package from the OCaml standard library or some other library, how I can find out which .cma file to load? In the standard library for example, String
is in str.cma
and Big_int
is in nums.cma
, so the filenames are not discernible from the module name or description.
Is there an easy way to look up the correct file for a module?
Typically, given an Ocaml library .cma
you can get the modules it defines using objinfo
(a.k.a. ocamlobjinfo
notably on Debian, Ubuntu, …). So, given library paths (/usr/lib/ocaml etc.) and time to spend, it should be possible to construct a mapping between modules and Ocaml library.
First, you don't really want to know which cma to load, rather you want to know which package to load via ocamlfind. Next thing to notice is that ocaml compilers need to perform the same thing to compile the project - i.e. by the name of the module referenced in source code find the compiled interface for that module. So let's emulate that behaviour. Compilers get the include paths from command-line, but we have to search all possible include paths. So here we go :
for i in $(ocamlfind list | cut -d ' ' -f 1) ; do
if [ -r $(ocamlfind query $i)/XXX.cmi ] ; then
echo $i; break;
fi ;
done
or
ocamlfind printconf path | xargs -n1 -I/ find / -name XXX.cmi
NB the mapping from module name to filename is not unique - e.g. SomeModule
can be represented either by someModule.cmi
or SomeModule.cmi
(less common).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With