如何链接ocamlopt生成的目标文件来创建可执行文件?

时间:2017-10-24 04:17:31

标签: ocaml

我可以使用以下ocaml模块test.ml并使用ocamlopt

进行编译
(* test.ml *)
Printf.printf "hi\n"

通过

$ ocamlopt -o test.native test.ml
$ ./test.native
hi

我也可以在没有链接的情况下编译它。

$ ocamlopt -o test.o -c test.ml
$ ls
test.cmi        test.cmx        test.ml         test.o

但是,如果我尝试将刚刚创建的对象与OCaml运行时链接以获取可执行文件,则不会创建任何文件(即使ocamlopt正常退出)

$ ocamlopt -o test.native test.o
$ ls
test.cmi        test.cmx        test.ml         test.o

如何指示ocamlopt链接到OCaml运行时并生成可执行文件"就像"我跑了ocamlopt -o test.native test.ml

1 个答案:

答案 0 :(得分:3)

您需要将.cmx文件提供给编译器,而不是.o文件。

$ ocamlopt -o test.native test.cmx
$ ./test.native
hi