7

I'm toying around with OCaml. The first thing I want to know how to do is build an OCaml project. Right now, I just want something stupidly simple since I'm just learning. Could anyone point me towards a build system along with a "hello world" type example for using that build system?

nlucaroni
  • 47,556
  • 6
  • 64
  • 86
Jason Baker
  • 192,085
  • 135
  • 376
  • 510

7 Answers7

13

ocamlopt is the standard native-code compiler. Typical build syntax is:

ocamlopt -o execname module1.ml module2.ml

You can use ocamlc to compile to bytecode instead.

Generally the standard library is already included. If you're on a *nix system, and want to include, say, the unix library, you'd do something like

ocamlopt -o execname unix.cmxa module1.ml module2.ml

cmxa files are native code libs, cma files are bytecode libs.

For more complicated builds, theres the ocamlfind program that helps locate libaries. You can also use GNU make and similar tools. More info can be found here. This is a page about using gmake.

This is an ok "Coder's First Ocaml" page.
Here is the lectures page for the Ocaml class I took back in college. Don't try hitting up Professor Gunter for any tips, though, she was an evil troll if memory serves.

phoebus
  • 14,673
  • 2
  • 33
  • 35
10

There is also ocamlbuild. For the simple project without external dependencies it is as simple as

ocamlbuild prog.native
ygrek
  • 6,656
  • 22
  • 29
  • this is a really easy way although the documentation sucks. Debugging the program using the toplevel compilation is helpful as well, "ocamlbuild prog.top" – nlucaroni Sep 28 '09 at 14:19
  • It helps. You can also find it here. https://realworldocaml.org/v1/en/html/a-guided-tour.html – eccstartup Feb 07 '14 at 14:57
  • 1
    In my experience, this is by far the easiest way to get started compiling multi-module Ocaml projects. – Nate C-K Feb 10 '14 at 06:24
4

Have a look at ocaml-make by Markus Mottle. The project includes OCamlMakefile. Copy this into your source directory and create a Makefile, e.g.:

SOURCES = hello.ml
RESULT  = hello
-include OCamlMakefile
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
3

You're probably looking for an ide. Take a look at Know of an OCAML IDE?.

http://www.ocaml-tutorial.org/compiling_ocaml_projects explains the compilation basics. At the bottom of that page there's a section called Automated build systems and links to the topics Compiling with GNU makeand Compiling with Omake both including samples.

Community
  • 1
  • 1
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • 1
    Actually, I prefer to use emacs. And for the "hello world" example, I was thinking more of a "hello world" type build script rather than a "hello world" for OCaml. :-) – Jason Baker Sep 27 '09 at 13:16
  • The answer with the most upvotes for question #118935 is "Emacs in Caml mode, or Tuareg mode", take a look ;-) – VolkerK Sep 27 '09 at 14:04
  • I've had a lot more luck with omake than with ocamlbuild - it's quite easy to list dependent packages in the omakefile; I couldn't figure out how to get that done with ocamlbuild. Recently I read on the ocaml mailing list that ocamlbuild is no longer supported. – aneccodeal Oct 23 '09 at 00:30
2

You probably know this, but just in case you don't, OCaml include a REPL that you can use to code interactively without needing to compile. It can be quite useful when you're first learning.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • You can type, #use "" to include a file when the toplevel is running. Don't for get to include extra (compiled) modules (.cma files) on the command line or, #load "filename.cma" in the toplevel. – nlucaroni Sep 28 '09 at 14:18
1

An up-and-coming project is OASIS, a pretty nifty build system being developed by Sylvain Le Gall. He uses it in a bunch of his OCaml projects, so do I.

Asherah
  • 18,948
  • 5
  • 53
  • 72
0

I would also add another Makefile for Ocaml, which I use for my projects.

The Makefile and example usage are available on the Ocaml website.

a_m0d
  • 12,034
  • 15
  • 57
  • 79