11

我是 Ocaml 编程的完整初学者,我无法将模块链接到我的程序中。实际上,我正在做一些正则表达式检查,并且我编写了一个函数,该函数基本上使用Str 模块基于分隔符字符串对字符串进行标记。所以我使用库中定义的函数,如下所示:

Str.regexp_string /*and so on*/

但是,当我尝试编译 ml 文件时,我收到一条错误消息,提示我有一个未定义的全局Str。我们通过键入 List.length 等来使用List函数,就像我对 Str 所做的那样,而不必显式包含特定模块。我试过了

open Str;;
include Str;; /*None of these work and I still get the same error*/

但是,如果在顶层我使用

load "str.cma" /*Then the program works without problems*/

我想将模块包含在 ml 文件中,因为我必须最后链接 3 cmo 才能获得最终的可执行文件(不在顶层运行)。我知道这是一个非常基本的问题,但我无法解决它。提前致谢。

4

4 回答 4

13

您无需在文件 foo.ml 中添加任何内容。在编译 foo.ml 时,您确实需要告诉编译器在哪里可以找到 Str 模块。为此,请将其添加到用于编译 foo.ml 的命令行中:

ocamlc str.cma foo.ml

或者

ocamlopt str.cmxa foo.ml

List和标准库中的其他模块默认是可以访问的,所以你不需要告诉编译器那些经常使用的模块。

于 2013-04-12T13:21:32.240 回答
1

只需添加str到文件的libraries字段中dune

于 2021-05-14T16:49:47.897 回答
0
ocamlc calc.ml str.cma -o calc
File "calc.ml", line 1:
Error: Error while linking calc.cmo:
Reference to undefined global `Str'

代码很简单,可以减少乱七八糟的。

let split_into_words s = 
    Str.split ( Str.regexp "[ \n\t]+") s ;;

let _ = 
    split_into_words "abc def ghi" ;;

在 ocaml 4.0.2 上。显然,这里有一个问题,但是我太初学者了,无法理解它是什么。从顶层看,#load "str.cma" 似乎可以正常工作,所以这里有些东西我们不明白。有人知道这是什么吗?

于 2013-12-12T14:23:53.510 回答
0

我认为您需要使用“-cclib”编译器指令。模块名称不应包含以 .cma 结尾的文件。下面是我在尝试使用 unix 和 threads 模块时所做的。我认为您需要使用“custom”和“cclib”编译器指令的某种组合。

ocamlc -custom unix.cma threa.ml -cclib -lunix

查看本书第 7 章的帮助: http ://caml.inria.fr/pub/docs/oreilly-book/html/book-ora063.html

并在这里查看编译器指令的覆盖范围:http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual022.html#c: camlc

于 2013-04-15T18:01:24.147 回答