如何使从OPAM安装的库对OCaml可用? [英] How to make library installed from OPAM available to OCaml?

查看:136
本文介绍了如何使从OPAM安装的库对OCaml可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OCaml FFI上遵循了本教程并安装了Ctypes通过OPAM:

I followed this tutorial on OCaml FFI and installed Ctypes through OPAM:

opam install ctypes

但是,OCaml找不到该模块:

However, OCaml does not find the module:

open Ctypes
(* ... *)

我收到错误:

Unbound module Ctypes

看起来我需要让OCaml知道我的Ctypes安装在哪里?我是否需要更新一些路径变量以让OCaml查找通过OPAM安装的库?

It looks like I need to let OCaml know where my Ctypes installation is? Do I need to update some path variable to let OCaml look for my libraries installed through OPAM?

这是Ubuntu 15.04,OCaml 4.01.0,OPAM 1.2.0.

This is Ubuntu 15.04, OCaml 4.01.0, OPAM 1.2.0.

推荐答案

在系统上安装某些内容并不能使其对编译器自动可见,这不仅适用于OCaml,而且适用于大多数常规系统,例如C或C ++仅举几例.

Installing something on your system doesn't make it automatically visible for the compiler, this is true not only for OCaml, but for most conventional systems, like C or C++ to name a few.

这意味着您需要将一些标志传递给编译器,编写Makefile或使用某些项目管理系统.

That means that you need to pass some flags to the compiler, or to write Makefiles, or to use some project management systems.

在OCaml中,我们拥有相当成熟的基础架构,尤其适用于opam.我不想深入解释,只是想快速浏览一下.

In OCaml we have quite a mature infrastructure that plays very well with opam in particular. I do not want to go very deeply in explanations, just a fast overview.

ocamlfind工具用于在系统上查找库.它在概念上与pkg-config有点相似,但在设计上却大不相同.它包装了编译器工具,以便将选项传递给它们.

ocamlfind tool is used to find libraries on your system. It is somewhat close to pkg-config in idea, but quite different in design. It wraps compiler tools in order to pass options to them.

ocamlbuild是花哨的瑞士刀,是每个OCamler武器库中必不可少的工具.它是一个了解所有其他工具以及如何将它们粘合在一起的工具.我想说这是编译项目的首选方法,尤其是小的项目.

ocamlbuild is a fancy swiss-knife that is a must have in the arsenal of every OCamler. It is a tool that knows all other tools, and how to glue them together. I would say that it is the preferred way to compile your projects, especially small one.

oasis在本质上与autotools相似,但不是通用的,而是在前提下编写的,即它应该非常易于使用.确实确实很容易,但仍然相当灵活和强大.

oasis is close to autotools in the spirit, but not that generic and is written in the premise, that it should be very easy to use. And indeed it is very easy, but still quite flexible and powerful.

考虑到这一概述,我们可以直接解决您的问题.因此,您已经安装了ctypes.现在让我们从ocamlfind角度看一下ctypes包在系统中的可见性.最简单的方法是列出所有对ocamlfind可见的软件包,并在其中找到ctypes:

With this overview in mind, we can go directly to your problem. So you've installed ctypes. Now let's take a look on how ctypes package is visible in your system from the ocamlfind perspective. The easiest way would be to list all packages, visible to ocamlfind and find ctypes there:

$ ocamlfind list | grep ctypes
ctypes              (version: 0.4.1)
ctypes.foreign      (version: 0.4.1)
ctypes.stubs        (version: 0.4.1)
ctypes.top          (version: 0.4.1)

因此,看起来在ctypes保护伞下有4个库.一个基本库和一些其他库提供了某些功能,这些功能默认情况下是不需要的.

So, it looks like, that under the ctypes umbrella there're 4 libraries. One basic library, and some extra libraries, that provides some functionality, that is not needed by default.

不,我们尝试将它们与ocamlbuild

No let's try to use them with ocamlbuild

ocamlbuild -package ctypes yourprogram.native

或者,在没有ocamlbuild的情况下直接与ocamlfind一起使用:

Or, without ocamlbuild directly with ocamlfind:

ocamlfind ocamlopt -package ctypes yourprogram.ml -o yourprogram.native

您可能会看到,有一个package选项,您可以将ocamlfind找到的软件包名称传递给该选项,并且编译器会自动看到它.

As you may see, there is a package option, to which you can pass a name of the package as found by ocamlfind, and it will be automagically made visible to the compiler.

这篇关于如何使从OPAM安装的库对OCaml可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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