OCaml:如何使用沙丘构建 mirage-tcpip?

OCaml: How to build mirage-tcpip using dune?

我正在尝试在文件夹 "examples" 中的 https://github.com/mirage/mirage-tcpip 处构建示例。

首先我在根目录下opam install . 安装了所有依赖项。然后我做了 dune build,它在 _build 中构建了所有内容,很多 .a, .cma, .cmx, .cmi, .cmxa 文件我不知道它们的用途(有人可以解释一下吗 ?).

无论如何,我认为示例必须单独构建,因为它们有自己的沙丘文件。但我对它们进行了 运行 dune build 尝试并得到:

root@66f08fd7c55b:/workspaces/ocaml_env/mirage-tcpip/examples/ping# dune build
Entering directory '/workspaces/ocaml_env/mirage-tcpip'
root@66f08fd7c55b:/workspaces/ocaml_env/mirage-tcpip/examples/ping# ls
dune  ping.ml
root@66f08fd7c55b:/workspaces/ocaml_env/mirage-tcpip/examples/ping# cat dune 
(executables
 (names ping)
 (libraries cmdliner logs logs.fmt tcpip.icmpv4-socket))
root@66f08fd7c55b:/workspaces/ocaml_env/mirage-tcpip/examples/ping# dune build ping
Entering directory '/workspaces/ocaml_env/mirage-tcpip'
Error: Don't know how to build ping

OCaml文件类型说明:

关于_build目录(reference 1, reference 2)中的文件类型:

  • .a 是包含本机代码的标准归档文件(也称为静态库):

    Arguments ending in .o or .a (.obj or .lib under Windows) are assumed to be C object files and libraries. They are passed to the C linker when linking in -custom mode (see the description of -custom below).

  • .cma 是等同于 .a 文件的 OCaml 字节码,因此是静态库,但使用 platform-independent OCaml 字节码而不是 machine-dependent本机代码:

    Arguments ending in .cma are taken to be libraries of object bytecode. A library of object bytecode packs in a single file a set of object bytecode files (.cmo files).

  • .cmx包含meta-data如何link本地代码对象文件在一起:

    Arguments ending in .ml are taken to be source files for compilation unit implementations. ... From the file x.ml, the ocamlopt compiler produces two files: x.o, containing native object code, and x.cmx, containing extra information for linking and optimization of the clients of the unit.

  • .cmi是编译好的接口定义文件:

    Arguments ending in .mli are taken to be source files for compilation unit interfaces. Interfaces specify the names exported by compilation units: they declare value names with their types, define public data types, declare abstract data types, and so on. From the file x.mli, the ocamlopt compiler produces a compiled interface in the file x.cmi.

  • .cmxa 是结合了 .a/.o.cmx 文件的本机代码库文件,所以像 .a 但是来自 .cmx 个文件的额外 linking 信息:

    Arguments ending in .cmxa are taken to be libraries of object code.

构建示例

关于 ping 示例的构建,要使用 dune 构建可执行目标,您必须将 .exe 附加到目标名称 (reference):

Note that native code executables will have the .exe extension on all platforms (including non-Windows systems).

因此完整的构建命令序列为:

  • 安装依赖项:

    $ opam install .
    
  • 构建 mirage-tcpip:

    $ dune build
    
  • 构建 ping 示例:

    $ cd example/ping
    $ dune build ping.exe
    
  • 运行 二进制:

    # dune exec ./ping.exe 8.8.8.8
    

    或者,从 mirage-tcpip 目录:

    # ./_build/default/examples/ping/ping.exe 8.8.8.8