Reputation:
I'm new to OCaml and I would like to know how can I write an ocaml code into a file and then compile it to run it whenever I want.
Now I'm using OCaml by typing ocaml
in the mac or linux terminal and writing the code, but when I'm done and I close the software I lose all the work.
Upvotes: 5
Views: 8815
Reputation: 3
If you are using Jane Street's Core, you should consider using the command corebuild
to compile, which includes a bunch of defaults after ocamlbuild
:
ocamlbuild \
-use-ocamlfind \
-pkg core \
-tag "ppx(ppx-jane -as-ppx)" \
-tag thread \
-tag debug \
-tag bin_annot \
-tag short_paths \
-cflags "-w A-4-33-40-41-42-43-34-44" \
-cflags -strict-sequence \
"$@"
I got this here.
Upvotes: 0
Reputation: 51
I'm thinking this tutorial - Compiling OCaml projects might help. It describes the basics of compiling OCaml. It discusses ocamlc
and ocamlopt
compilers in depth and other compiler tools.
Upvotes: 5
Reputation: 35210
There're plenty of options, but the easiest one (to my opinion) is to use ocamlbuild
. If you have your code in file program.ml
, then
ocamlbuild program.native
will compile your program into a native binary, so that you can run it as simple as:
./program.native
There is also a shortcut that allows you to compile and run a program as one action:
ocamlbuild program.native --
You can pass arguments to your program after the --
sign.
If your program consists of more than one file, that's not a problem, as ocamlbuild
will scan it, and automatically build all dependencies in a correct order.
If your program requires external libraries, then you can specify them with a -pkg
or -pkgs
option. Like this:
ocamlbuild -pkg lwt program.native
Upvotes: 10