views:

344

answers:

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?

+3  A: 

You're probably looking for an ide. Take a look at http://stackoverflow.com/questions/118935/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.

VolkerK
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
The answer with the most upvotes for question #118935 is "Emacs in Caml mode, or Tuareg mode", take a look ;-)
VolkerK
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
+6  A: 

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
I've had my fair share of evil troll professors. Point noted. :-)
Jason Baker
+3  A: 

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
+2  A: 

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
You can type, #use "<filename.ml>" 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
+3  A: 

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

ocamlbuild prog.native
ygrek
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
A: 

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