[英]Use ocamlfind with ocaml interpreter, without compiling
I have a file main.ml
that uses some package that makes the command ocaml main.ml
fail and report this error: 我有一个文件
main.ml
,它使用一些使ocaml main.ml
命令失败的包并报告此错误:
Error: Unbound module X
And the only solution I've found so far is to run: 到目前为止,我发现的唯一解决方案是运行:
ocamlbuild main.native -use-ocamlfind -package X
./main.native
But this compiles the file. 但这会编译文件。 Is there a way to just run the regular ocaml interpreter (
ocaml main.ml
) with some additional flags to make it find the package? 有没有一种方法可以运行带有一些附加标志的常规ocaml解释器(
ocaml main.ml
),以使其找到包?
The easiest way is probably to use topfind and add #require "core"
at the top of your file. 最简单的方法可能是使用topfind并在文件顶部添加
#require "core"
。 If you want to avoid adding toplevel directives in your file, you can use ocamlfind query -i-format -r
to compute the required include directories: 如果要避免在文件中添加顶级指令,则可以使用
ocamlfind query -i-format -r
计算所需的包含目录:
ocaml $(ocamlfind query -i-format -r core) main.ml
You can put at the beginning of your file some top-level directives which find and load needed modules. 您可以在文件的开头放置一些顶级指令,这些指令可以查找和加载所需的模块。 For example, when I want to use
Core
library in my script I insert: 例如,当我想在脚本中使用
Core
库时,插入:
#use "topfind";;
#thread;;
#require "core";;
open Core.Std
(* Code of my script *)
To use topfind
package ocamlfind
has to be installed ( opam install ocamlfind
) 要使用
topfind
包ocamlfind
必须安装( opam install ocamlfind
)
You can also query the path of your package to ocamlfind : 您还可以查询包到ocamlfind的路径:
ocamlfind query package_X
That will return the path of your package : /path_to/packageX
这将返回您的包的路径:
/path_to/packageX
And then : 接着 :
ocaml main.ml -I /path_to/packageX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.