在OCaml中的哪里放置共享实用程序模块? [英] Where to place a shared utility module in OCaml?

查看:62
本文介绍了在OCaml中的哪里放置共享实用程序模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件Tools.ml,其中包含一些我自己编写的常用实用程序函数.在.../Code/下,我有几个文件夹,每个文件夹包含一个项目.我的问题是,应该将该Tools.ml放在哪里,以便.../Code/下的所有文件夹和文件都可以由Open Tools共享此模块.

I have a file Tools.ml which contains some common utility functions I write myself. Under .../Code/ I have several folders which each contains a project. My question is where I should place this Tools.ml such that all the folders and files under .../Code/ could share this module by Open Tools.

希望我的问题很清楚...有人解决方案好吗?

Hope my question is clear... Does anyone have a good solution?

Edit1 :按照@gasche的回答,我写了tools.ml如下:

Following @gasche's answer, I have written tools.ml as follows:

module Tools =
  struct
    let a_function = ...
    ...
  end

然后我编译了该文件,并按照建议完成了ocamlfind install tools META tools.cmo tools.cmx tools.ml,看起来一切正常.然后我写了test.ml如下:

Then I compiled it, and done ocamlfind install tools META tools.cmo tools.cmx tools.ml as suggested, which looks going well. Then I have written test.ml as follows:

open Tools

let f = Tools.a_function

然后用ocamlc test.ml -o test进行编译,然后出现错误:

then I compiled it with ocamlc test.ml -o test, then I got an error:

File "test.ml", line 1, characters 0-1:
Error: Error while linking test.cmo:
Reference to undefined global `Tools'

谁能告诉我发生了什么事?

Could anyone tell me what happened?

推荐答案

您可以将其打包为一个独立的库,与其他OCaml库一起安装,然后从您的项目中以库的形式对其进行访问.

You could package it as an independent library, install it with other OCaml libraries, and access to it, from your project, as a library.

一种非常简单的方法是为ocamlfind编写一个META文件.在您可以容纳个人图​​书馆"项目的地方创建目录.假设您有tools.mltools.mli,并且您的代码取决于某些findlib程序包(例如unixbigarray).您的META看起来像这样:

A very simple way to do this is to write a META file for ocamlfind. Create a directory somewhere you're comfortable to hold you "personal library" project. Suppose you have tools.ml and tools.mli, and your code depends on some findlib package (eg. unix and bigarray). You META would look like this:

name="tools"
description="personal collection of utilities"
version="0.1"
requires="unix,bigarray"
archive(byte)="tools.cmo"
archive(native)="tools.cmx"

一旦编写了此META文件,就很容易要求ocamlfind安装"该库(如果需要,可以将其删除),并在其他项目中使用它.要安装,语法为ocamlfind install <name> <meta-file> <file1> <file2> ...,其中<file1>, <file2>..是您希望看到的包含在安装目录中的文件.您至少必须具有tools.cmi tools.cmo(对于本机编译,必须具有tools.otools.cmx),但是例如也具有tools.mli是一种很好的做法(如果要提供代码,请tools.ml).

Once you have written this META file, it is easy to ask ocamlfind to "install" the library (and remove it if you want to), and use it in your other projects. To install, the syntax is ocamlfind install <name> <meta-file> <file1> <file2> ... where <file1>, <file2>.. are the file you wish to see included in the installation directory. You must at least have tools.cmi tools.cmo (and tools.o and tools.cmx for native compilation), but it is good practice to also have tools.mli for example (and, if you want to provide the code, tools.ml).

ocamlfind install tools META tools.cmi tools.cmo tools.o tools.cmx tools.mli

(当然必须存在tools.cmo等,也就是说,编译软件包后必须install.如果使用ocamlbuild,则它们很可能位于_build子目录中,因此ocamlfind install ... _build/tools.cmo ....)

(Of course tools.cmo etc. have to exist, that is you must install after you have compiled your package. If you have used ocamlbuild, they are likely to be in a _build subdirectory, so ocamlfind install ... _build/tools.cmo ....)

在众多项目中,您可以轻松地使用您的库,或者直接使用ocamlfind toold编译库(如果您已经这样做,则可以编译程序)

From your numerous projects, you can use your library easily, either using the ocamlfind toold directly if this is what you already do to compile your programs

ocamlfind ocamlc -package tools ....

或通过ocamlbuild提供的功能,例如,在标签中添加package(tools).

or through the facilities provided by ocamlbuild for example, adding package(tools) to your tags.

如果对库进行了更改并希望从项目中对其进行访问,则要重新安装该库

To reinstall your library if you made a change to it and want it accessible from your projects

ocamlfind remove tools
ocamlfind install tools META ...

您还可以通过oasis处理所有这一切,这是ocamlfind/ocamlbuild之上的一个层,用于自动执行此过程.我对oasis不够熟悉,无法给出这样的例子,但是对于这种受限制的情况(一个文件库)应该同样简单,并且如果以后希望扩展您的库,则可以更好地扩展(例如,它还可以处理文档生成,预编译配置...).

You could also handle all this through oasis, which is a layer on top of ocamlfind/ocamlbuild to automate this process. I'm not familiar enough with oasis to give such examples off the top of my head, but it should be equally simple for such a restricted case (one-file library), and scale better if you wish later to extend your library (eg. it can also handle documentation generation, pre-compilation configuration...).

这篇关于在OCaml中的哪里放置共享实用程序模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆