What's a stupidly simple way to compile an OCaml project?
Asked Answered
O

7

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?

Oringas answered 27/9, 2009 at 13:1 Comment(0)
D
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.

Discoverer answered 27/9, 2009 at 13:15 Comment(1)
I've had my fair share of evil troll professors. Point noted. :-)Oringas
D
10

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

ocamlbuild prog.native
Drench answered 28/9, 2009 at 7:46 Comment(3)
this is a really easy way although the documentation sucks. Debugging the program using the toplevel compilation is helpful as well, "ocamlbuild prog.top"Treblinka
It helps. You can also find it here. realworldocaml.org/v1/en/html/a-guided-tour.htmlAbnormality
In my experience, this is by far the easiest way to get started compiling multi-module Ocaml projects.Wakayama
S
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
Siddon answered 27/9, 2009 at 14:17 Comment(0)
A
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.

Apparitor answered 27/9, 2009 at 13:11 Comment(3)
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. :-)Oringas
The answer with the most upvotes for question #118935 is "Emacs in Caml mode, or Tuareg mode", take a look ;-)Apparitor
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.Painless
R
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.

Rompers answered 27/9, 2009 at 20:3 Comment(1)
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.Treblinka
D
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.

Double answered 7/11, 2010 at 2:9 Comment(0)
G
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.

Greenroom answered 28/7, 2010 at 1:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.