Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage compilation well in OCaml?

I am learning more complex compilations in OCaml.

first I haven't been a C programmer and I really don't know what is make, etc. I am using Mac OS X terminal and i am also a Java programmer.

I find that in OCaml, there some things like ocamlc, ocamlbuild, ocamlfind, oasis, etc. I got very confused by them.


Question 1

Can anyone tell me which one I should use among ocamlc, ocamlbuild, ocamlfind, oasis?


Question 2

I tried this tutorial http://nicolaspouillard.fr/ocamlbuild/ocamlbuild-user-guide.html, it is good for ocamlbuild.

But if I want to use external libraries such as ocaml-batteries or camomile, how can I link those libraries using ocamlbuild?


Question 3

This is a more practical question about all the external libraries.

So for many ocaml libraries, I use opam install to install them.

  1. why need to install a library? I mean, in Java, normally we just copy a lib to somewhere and then include the path of the lib into -classpath or -cp. then why we need to install a OCaml library?

  2. after opam install a lib, such as camomile (for utf8), what happened and what will happen? Is this kind of install just download sourcefiles of a lib and copy it to somewhere?

  3. how can find the library then? for example, if I opam install camomile, then how can I link or use them in my own code?

  4. normally how to use a ocaml library? for example, for camomile (http://camomile.sourceforge.net/dochtml/index.html) they have three modules: CamomileLibrary, etc. So I should open the module in my code, right?

like image 665
Jackson Tale Avatar asked Nov 03 '22 23:11

Jackson Tale


1 Answers

Tools:

  • ocamlc: OCaml to bytecode compiler
  • ocamlopt: OCaml to native code compiler
  • ocamlfind: wrapper around ocamlc and ocamlopt to compile/link with various OCaml packages, i.e. you use "ocamlfind ocamlopt -package camomile -c yourfile.ml' to compile with yourfile.ml, and you use 'ocamlfind ocamlopt -package camomile yourfile.cmx -linkpkg -o yourfile' to create an executable
  • oasis: a build system generator like autoconf&automake for C/C++ but much simpler

With a recent ocamlbuild you can use 'ocamlbuild -use-ocamlfind -pkg -pkg ... ' as quick way of building your project with ocamlfind packages package1, package2. A next step would be to use 'ocamlbuild -use-ocamlfind ' and put package() directives in a _tags file.

However I would recommend trying oasis, it simplifies creating a build system for your project. If you want to see what happens "under-the-hood" when using oasis or ocamlbuild see the _build/_log file. It contains all the ocamlfind/ocamlc/ocamldep/ocamlopt invocations.

The opam question probably belongs into a separate question.

like image 106
Török Edwin Avatar answered Nov 09 '22 09:11

Török Edwin