Compiling and running in OCaml

北慕城南 提交于 2019-12-23 10:09:42

问题


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.


回答1:


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



回答2:


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.




回答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.



来源:https://stackoverflow.com/questions/34028789/compiling-and-running-in-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!