Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an OCaml library

I am trying to create a library that I can use in other OCaml projects, and I'm totally lost.

I'm currently using ocamlbuild which is great for spitting out executables, but I don't know how to get a library out of it.

I've discovered the -a option in ocamlopt and ocamlc but I'm not really sure how to use it. The documentation I've found (for example, here), seems to assume some preexisting knowledge. I don't even know what a .a file is. After I run that, which of the outputted files do I need to build a project that depends on this library? Do I need the mli files so that the application knows the signatures of the library code, or is that included in the output somehow? Also, it would be nice to be able to package all the files together, something similar to a .jar file for Java.

In any case, I would love for ocamlbuild to do all of this for me, since if I have to invoke ocamlopt -a I will have to either manually specify dependencies or hack a script around ocamldep -- something that ocamlbuild was supposed to fix. However, I don't know how to tell it to build a library.

I'm willing to use oasis or OPAM or something if it's necessary, but I would like to learn how to do this using just the basic tools first.

like image 800
Nat Mote Avatar asked May 26 '14 01:05

Nat Mote


1 Answers

OCamlbuild has some built-in functionality for building libraries, so you can get started with just ocamlbuild foo.cma foo.cmxa (assuming foo.ml is your entry point). This will invoke ocamlopt -a and ocamlc -a for you, handling all the dependency plumbing and leaving the generated files inside _build.

That should be enough to let you compile a library and link it from another program. Since this is just a test you can simply point at the aforementioned _build with -I when compiling the program that uses the library. For real use a library should be packaged - when you get to that point you'll want to look into ocamlfind, oasis, etc.

Have a look at the ocaml.org tutorial on compiling OCaml projects. Additionally the official manual for the bytecode and native code compilers contains useful detail on producing and using the various types of files.

like image 59
gsg Avatar answered Nov 17 '22 10:11

gsg