单独编译OCaml模块 [英] Separate compilation of OCaml modules
问题描述
我已阅读此问题以及其他内容, 但是我的编译问题没有解决.
I have read this question and others, but my compile problem is unsolved.
我正在使用这些文件测试单独的编译:
I am testing separate compilation with these files:
testmoda.ml
testmoda.ml
module Testmoda = struct
let greeter () = print_endline "greetings from module a"
end
testmodb.ml
testmodb.ml
module Testmodb = struct
let dogreet () = print_endline "Modul B:"; Testmoda.greeter ()
end
testmod.ml
testmod.ml
let main () =
print_endline "Calling modules now...";
Testmoda.greeter ();
Testmodb.dogreet ();
print_endline "End."
;;
let _ = main ()
现在我生成.mli文件
Now I generate the .mli file
ocamlc -c -i testmoda.ml >testmoda.mli
那里是testmoda.cmi.
and the testmoda.cmi is there.
下一步,我创建无错误的.cmo文件:
Next I create the .cmo file without errors:
ocamlc -c testmoda.ml
很好,对testmodb.ml也是如此:
Fine, so do the same with testmodb.ml:
strobel@s131-amd:~/Ocaml/ml/testmod> ocamlc -c -i testmodb.ml >testmodb.mli
File "testmodb.ml", line 3, characters 45-61:
Error: Unbound value Testmoda.greeter
另一种尝试:
strobel@s131-amd:~/Ocaml/ml/testmod> ocamlc -c testmoda.cmo testmodb.ml
File "testmodb.ml", line 3, characters 45-61:
Error: Unbound value Testmoda.greeter
其他组合也失败了.
如何编译testmodb.ml和testmod.ml?这应该很容易-没有ocamlbuild/omake/ 我认为是绿洲.
How do I compile testmodb.ml and testmod.ml? This should be easy - without ocamlbuild / omake / oasis, I think.
排除文件中的语法错误, 如果我将它们组合在一起到一个文件(之间有所需的空间),它将进行编译 并完美执行.
Syntax errors in the files are excluded, if I cat them together to one file (with the required space between) it compiles and executes perfectly.
推荐答案
OCaml在每个源文件的顶层为您提供了免费的模块.因此,您的第一个模块实际上被命名为Testmoda.Testmoda
,该函数被命名为Testmoda.Testmoda.greeter
,依此类推.如果您的文件仅包含函数定义,那么效果会更好.
OCaml gives you a module for free at the top level of each source file. So your first module is actually named Testmoda.Testmoda
, the function is named Testmoda.Testmoda.greeter
, and so on. Things will work better if your files just contain the function definitions.
作为补充,如果您要使用ocamlc -i
生成的界面,则实际上不需要mli文件.没有mli文件的接口与ocamlc -i
生成的接口相同.如果您不希望使用默认界面,则使用ocamlc -i
可以为您的mli文件提供一个很好的起点.但是对于这样一个简单的示例,它只会使事情看上去比实际情况复杂得多(恕我直言).
As a side comment, if you're going to use the interface generated by ocamlc -i
, you really don't need mli files. The interface in the absence of an mli file is the same as the one generated by ocamlc -i
. If you don't want the default interface, using ocamlc -i
gives a good starting point for your mli file. But for a simple example like this, it just makes things look a lot more complicated than they really are (IMHO).
如果您按照我的描述修改了文件(删除了多余的模块声明),则可以按如下所述从头开始编译并运行:
If you modify your files as I describe (remove extra module declarations), you can compile and run from scratch as follows:
$ ls
testmod.ml testmoda.ml testmodb.ml
$ cat testmoda.ml
let greeter () = print_endline "greetings from module a"
$ cat testmodb.ml
let dogreet () = print_endline "Modul B:"; Testmoda.greeter ()
$ ocamlc -o testmod testmoda.ml testmodb.ml testmod.ml
$ ./testmod
Calling modules now...
greetings from module a
Modul B:
greetings from module a
End.
如果已经使用ocamlc -c file.ml
编译了文件,则可以在上述命令中将.ml
替换为.cmo
.即使所有文件名都是.cmo
文件,此方法也有效;在这种情况下,ocamlc只是为您将它们链接在一起.
If you have already compiled a file (with ocamlc -c file.ml
) you can replace .ml
with .cmo
in the above command. This works even if all the filenames are .cmo
files; in that case ocamlc just links them together for you.
这篇关于单独编译OCaml模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!