Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 链接ocaml中的模块时出错_String_Module_Include_Ocaml - Fatal编程技术网

String 链接ocaml中的模块时出错

String 链接ocaml中的模块时出错,string,module,include,ocaml,String,Module,Include,Ocaml,我是Ocaml编程的完全初学者,在将模块链接到我的程序时遇到困难。实际上,我正在做一些正则表达式检查,并且我已经编写了一个函数,它基本上使用。因此,我使用库中定义的函数,如下所示: Str.regexp_string /*and so on*/ 然而,当我试图编译ml文件时,我得到一个错误,提示我有一个未定义的全局Str。我们通过键入List.length等来使用列表函数,就像我对Str所做的那样,而不必显式地包含特定的模块。我试过了 open Str;; include Str;; /*No

我是Ocaml编程的完全初学者,在将模块链接到我的程序时遇到困难。实际上,我正在做一些正则表达式检查,并且我已经编写了一个函数,它基本上使用。因此,我使用库中定义的函数,如下所示:

Str.regexp_string /*and so on*/
然而,当我试图编译ml文件时,我得到一个错误,提示我有一个未定义的全局Str。我们通过键入List.length等来使用列表函数,就像我对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才能获得最终的可执行文件(它不是在顶层运行的)。我知道这是一个非常基本的问题,但我解决它有困难。提前感谢。

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

ocamlc str.cma foo.ml


List
和标准库中的其他模块在默认情况下是可访问的,因此您不需要告诉编译器那些常用的模块。

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

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

请参阅本书第7章以获取帮助:

并在此处查看编译器指令的覆盖范围:

代码非常简单,可以减少垃圾

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

let _ = 
    split_into_words "abc def ghi" ;;

在OCAML4.0.2上。显然,这里有一个问题,但我是一个太多的初学者,不明白它是什么。从顶层来看,它似乎可以与#load“str.cma”配合使用,所以这里有一些我们不理解的地方。有人知道它是什么吗?

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

我认为你的解决方案行不通,因为他提到他想链接最终生成的“.cmo”文件。所以如果我试着编译一个简单的,使用“Str”模块的一些函数,然后像你说的那样编译它。如果我在顶层加载“.cmo”,我仍然会得到一个错误,说“未定义全局标准”。问题是关于链接一个独立程序。toplevel的答案略有不同。在toplevel中,您可以通过说
#load“Str.cma”
来加载Str。。。。或者使用
ocaml str.cma
调用
ocaml
顶级。只需在“库”中添加“str”对dune BTW有效。您需要将
str.cma
放在
calc.ml
之前。您知道如何使用
Std
解决此问题吗?我在这里遇到了同样的问题,但只是关于
Std
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" ;;