我有一个 OCaml 项目使用dune
按照基本教程中的建议,我有一个目录结构,例如:
bin/
cli.ml
dune
lib/
dune
...
<various>.ml
Run Code Online (Sandbox Code Playgroud)
我的目录中的文件数量lib
不断增加,我希望有另一个级别的命名空间。
我想要子目录,例如:
lib/
utils/
dune
...
<various>.ml
some_other_domain/
dune
...
<various>.ml
dune
...
<various>.ml
Run Code Online (Sandbox Code Playgroud)
我希望能够像这样打开它们Lib.Utils.Whatever
我想这一定是可能的吗?
我尝试dune
在下面创建一个文件lib/utils
:
bin/
cli.ml
dune
lib/
dune
...
<various>.ml
Run Code Online (Sandbox Code Playgroud)
...但open Lib.Utils.Whatever
似乎不起作用。
我找到了该subdir
节...但是如果我将其添加到lib/dune
并定义utils
为子目录library
,那么我就不会得到命名空间...我必须open Utils
这样做而不是open Lib.Utils
根据https://github.com/ocaml/ocaml/pull/1110,OCaml 4.08 及更高版本会对未使用的模块打开发出警告,即使它们是使用open!
. open! Foo
这与使用来确定以下代码位于模块上下文中的常见做法产生了摩擦Foo
(无论是否从内部使用任何东西Foo
)。此外,由于dune
默认情况下将所有警告视为致命错误,这将导致默认dev
配置文件中的沙丘构建失败,并出现类似错误
$ dune build
File "lib/mylib.ml", line 1, characters 0-10:
1 | open! Core
^^^^^^^^^^
Error (warning 66): unused open! Core.
Run Code Online (Sandbox Code Playgroud)
如何禁用此警告和致命错误?
当我尝试打电话#require "core"
时utop
,我收到一个错误:
utop # #require "core";;
Exception: Unix.Unix_error (Unix.ENOENT, "sysconf", "").
Raised by primitive operation at unknown location
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
Run Code Online (Sandbox Code Playgroud)
我有一个.ocamlinit
在我当前目录中看起来像这样的文件:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#thread;;
#camlp4o;;
Run Code Online (Sandbox Code Playgroud)
可能导致此错误发生的原因是什么?它似乎是Findlib的一个错误?
我试图抹着我的~/.opam
目录,重新运行opam init
,opam switch set 4.05.0
以及重新安装core
和utop
,但这个错误仍然存在.是否有可能我还需要重新安装opam
?我目前正在使用opam-1.2.2的二进制版本.
我正在运行Arch Linux,我相信在更新系统上的所有软件包之后会出现这个错误,但我不确定.
我仍然可以用来jbuilder
构建可执行文件,但是在运行可执行文件时,它们会因类似的错误而失败:
$ _build/install/default/bin/my_executable
Uncaught …
Run Code Online (Sandbox Code Playgroud) 我正在使用沙丘建立一个演示项目,并想将可执行文件命名为“hello-world.exe”而不是“hello_world”。如果我尝试构建项目,我会收到编译器错误。如何在文件名中使用连字符声明可执行文件?
我正在使用沙丘 1.9.3 和 OCaml 4.05.0。
沙丘:
(executable
(name hello-world))
Run Code Online (Sandbox Code Playgroud)
你好-world.ml
(executable
(name hello-world))
Run Code Online (Sandbox Code Playgroud)
构建错误
let () = print_endline "Hello, World!"
Run Code Online (Sandbox Code Playgroud) 假设我有一个具有以下结构的简单项目:
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dune-project\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 bin\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dune\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.ml\n
Run Code Online (Sandbox Code Playgroud)\ndune-project
(lang dune 2.7)\n(name myproject)\n\n(package\n (name myproject)\n (synopsis "myproject")\n (description "Basic project")\n)\n
Run Code Online (Sandbox Code Playgroud)\ndune
(executable\n (name main)\n (public_name myproject)\n)\n
Run Code Online (Sandbox Code Playgroud)\n编译时,我有一个具有以下结构的dune build
新目录:_build
_build\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 default\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dune-project\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 META.myproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 myproject.dune-package\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 myproject.install\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 bin\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dune\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.exe\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.ml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 install\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 default\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bin\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myproject -> ../../../default/src/bin/main.exe\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 lib\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dune-package -> ../../../../default/myproject.dune-package\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 META -> …
Run Code Online (Sandbox Code Playgroud) 我在运行 OUnit 测试时遇到困难,主要是因为我对沙丘和 OUnit 都不熟悉。dune
我跑步时抱怨dune runtest
:
File "test/dune", line 4, characters 13-14:
Error: Library "f" not found.
Hint: try: dune external-lib-deps --missing @runtest
Run Code Online (Sandbox Code Playgroud)
这是项目结构:
??? dune
??? f.ml # This is the source file.
??? test
??? dune
??? f_test.ml # This is the test.
Run Code Online (Sandbox Code Playgroud)
这是dune
:
(executable
(name f))
Run Code Online (Sandbox Code Playgroud)
这是test/dune
:
(test
(name f_test)
(libraries oUnit f)) ; <- `f` here causes problems.
Run Code Online (Sandbox Code Playgroud)
我可以看到错误出现是因为沙丘不知道 about f.ml
,因此f
在沙丘文件中不知道。
如何f.ml
以test/dune …
对于单个文件可执行文件,如何加载它dune utop
?
lib
准确地说,在目录中有库和相应文件的项目上dune
,我可以启动dune utop lib
,open Lib.MyModule
这将加载文件MyModule.ml
。
对于没有子目录且根目录下有以下文件的项目来说,相当于一个dune
文件
(executable
(name auth)
(libraries ezjsonm cryptokit))
Run Code Online (Sandbox Code Playgroud)
和一个文件auth.ml
let hi = "value"
Run Code Online (Sandbox Code Playgroud)
我想从该文件中加载值dune utop
我正在做一个项目,我需要尽可能地优化我的代码,因为正常编译需要几个小时才能运行。
有人告诉我使用 Flambda,但我在互联网上没有找到任何有关如何将其与沙丘一起使用的信息。
我希望得到遵循的说明/阅读的资源。
我第一次尝试OCaml并试图一起构建几个文件.
当我跑:
dune build bin/main.exe
Run Code Online (Sandbox Code Playgroud)
我正进入(状态:
ocamlc bin/.main.eobjs/main.{cmi,cmo,cmt} (exit 2)
(cd _build/default && /usr/bin/ocamlc.opt -w @a-4-29-40-41-42-44-45-48-58-59-60-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -bin-annot -I bin/.main.eobjs -I lib/.lib.objs -no-alias-deps -opaque -o bin/.main.eobjs/main.cmo -c -impl bin/main.ml)
File "bin/main.ml", line 10, characters 17-25:
Error: Unbound module Rule
Run Code Online (Sandbox Code Playgroud)
这是我的bin/main.ml文件:
open Lib
let () =
let result = Math.add 2 3 in
print_endline (string_of_int result);
let result = Math.sub 3 1 in
print_endline (string_of_int result);
let result = Zoom.barf 3 1 in
print_endline (string_of_int result);
let …
Run Code Online (Sandbox Code Playgroud) ocaml ×10
ocaml-dune ×10
opam ×2
utop ×2
compilation ×1
debugging ×1
ocaml-core ×1
optimization ×1
ounit ×1