问题描述
我是OCaml的新手,我想知道如何将ocaml代码写入文件,然后编译该文件以在需要时运行它.现在,我通过在Mac或Linux终端中键入ocaml
并编写代码来使用OCaml,但是当我完成并关闭软件时,我将丢失所有工作.
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.
推荐答案
有很多选择,但最简单的选择(我认为)是使用.如果您的代码位于文件program.ml
中,则
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.
如果您的程序包含多个文件,这不是问题,因为ocamlbuild
将对其进行扫描,并以正确的顺序自动构建所有依赖项.
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.
如果程序需要外部库,则可以使用-pkg
或-pkgs
选项指定它们.像这样:
If your program requires external libraries, then you can specify them with a -pkg
or -pkgs
option. Like this:
ocamlbuild -pkg lwt program.native
这篇关于在OCaml中编译并运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!