编译多个Ocaml文件 [英] Compiling multiple Ocaml files
问题描述
我是Ocaml的新手,试图写一些小的示例应用程序。我在Linux Ubuntu 10.04下使用 ocamlc
3.11.2版本。我想编译两个文件:
a.ml
b.ml
文件 b.ml
使用 a.ml
。据我所知,我可以使用 ocamlc -c
仅执行编译。我可以调用 ocamlc
最后一次,当我有所有的 .cmo
文件将它们链接到一个可执行文件。此外,当编译使用来自另一个文件的定义的文件时,我必须告诉编译器在 .cmi
文件中找到外部定义。
所以我的想法是使用:
ocamlc -i -c a.ml> a.mli
ocamlc -c a.mli b.ml
ocamlc -ob a.cmo b.cmo
第一步工作并生成文件 a.mli
和 a.cmo
,但当运行第二步我得到
文件b.ml,第1行,字符28-31:
错误:未绑定值foo
其中 foo
一个在 a.ml
中定义并在 b.ml
中调用的函数。
所以我的问题是:如何单独编译每个源文件并指定要在命令行上导入的接口?我一直在文档中查找,根据我可以理解,我必须指定 .mli
文件被包括,但我不知道如何。
EDIT
这里有一些详细信息。文件 a.ml
包含定义
let foo = 5 ;;
文件 b.ml
/ p>
print_string(string_of_int foo)^\\\
;;
真实的例子是更大的,但是这些文件我已经有上面报告的错误。 >
EDIT 2
我已编辑文件 b.ml
并替换 foo
与 A.foo
$ c> b.ml 即使我有另一个编译错误,这对于这个问题不重要)。我想根据
.mli
div> 如果你显示的代码不工作会更清楚。正如Kristopher指出的,但是,最可能的问题是你不是defineig哪个模块 foo
在。你可以明确指定模块, A.foo
。或者您可以打开A
,只需使用名称 foo
。
对于一个小例子没关系,但对于一个大项目,你应该小心不要使用 open
太自由。你希望在你的模块中自由地使用好的名字,如果你打开太多的名字,好名字可能会彼此冲突。
I am new to Ocaml and trying to write some small example application. I am using ocamlc
version 3.11.2 under Linux Ubuntu 10.04. I want to compile two files:
a.ml
b.ml
File b.ml
uses definitions from a.ml
. As far as I understand, I can use ocamlc -c
to perform compilation only. I can call ocamlc
one final time when I have all the .cmo
files to link them to an executable. Also, when compiling a file that uses definitions from another file, I have to tell the compiler in which .cmi
file to find the external definitions.
So my idea was to use:
ocamlc -i -c a.ml > a.mli
ocamlc -c a.mli b.ml
ocamlc -o b a.cmo b.cmo
The first step works and produces files a.mli
and a.cmo
, but when running the second step I get
File "b.ml", line 1, characters 28-31:
Error: Unbound value foo
where foo
is a function that is defined in a.ml
and called in b.ml
.
So my question is: how can I compile each source file separately and specify the interfaces to be imported on the command line? I have been looking in the documentation and as far as I can understand I have to specify the .mli
files to be included, but I do not know how.
EDIT
Here some more details. File a.ml
contains the definition
let foo = 5;;
File b.ml
contains the expression
print_string (string_of_int foo) ^ "\n";;
The real example is bigger but with these files I already have the error I reported above.
EDIT 2
I have edited file b.ml
and replaced foo
with A.foo
and this works (foo is visible in b.ml
even though I have another compilation error which is not important for this question). I guess it is cleaner to write my own .mli
files explicitly, as suggested by
It would be clearer if you showed the code that's not working. As Kristopher points out, though, the most likely problem is that you're not specifyig which module foo
is in. You can specify the module explicitly, as A.foo
. Or you can open A
and just use the name foo
.
For a small example it doesn't matter, but for a big project you should be careful not to use open
too freely. You want the freedom to use good names in your modules, and if you open too many of them, the good names can conflict with each other.
这篇关于编译多个Ocaml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!