问题
I am currently trying to load a png
image file into OCaml
. I am trying to use the Png.load
function but when I use it OCaml
gives me the error:
Unbound module Png merlin.
I have CamlImages
downloaded. I am using
- OCaml version 4.08.1
camlimages version 5.0.1 and I'm on
macOS Catalina version 10.15.1.
I'd really appreciate any help or any information anyone could give me. I've been trying to fix solve this issue for weeks.
回答1:
To use a package in OCaml you have to perform two actions:
- install the package
- tell your build system to use the package
This is a general guideline, details may vary since OCaml is a mature system with many options to choose from, there are many package managers and lots of build systems. I will start with the most common.
Installing a package
opam
If you're using OCaml Package Manager (opam), then you can install your package using opam install <pkgname>
, e.g.,
opam install camlimages
Do not forget to activate opam, with
eval $(opam env)
as opam installs packages locally and you need to setup the environment correctly so that your build system can see it. This is what eval $(opam env)
is doing.
Building with a package
Building with ocamlbuild
OCamlBuild is venerable but still quit popular tool for building OCaml programs. It is very easy to use and doesn't ask too many questions. It doesn't scale well to large projects though. Provided that your application main file is app.ml
, the build command is very simple
ocamlbuild -pkg camlimages.all app.native
The command will automatically scan the local dependencies and build them, as well as link your application with the camlimages
package. You can add more packages, using the -pkgs
flag, e.g.,
ocamlbuild -pkgs camlimages.all,core_kernel app.native
Since OCamlBuild is scanning your folder for files you should create a fresh new folder for each new folder. OCamlBuild doesn't like any leftover or junk files in your folder. It may even complain about them and create a script that will remove them.
Configuring Merlin
Merlin is a build system of its own, so it also needs some setup. The easiest way is to create a .merlin
file in the top folder of your project and for each package that you want to use add a line PKG <pkgname>
, e.g.,
PKG camlimages.all
Packages vs Libraries
In OCaml a package is a collection of libraries. In general, the names of libraries that constitute a package may be different from the name of the package. This usually raises a lot of confusion. In the case of camlimages, we have a package camlimages
which has a lot of libraries, e.g.,
$ ocamlfind list | grep camlimages
camlimages (version: 4.2.6)
camlimages.all (version: 4.2.6)
camlimages.all_formats (version: 4.2.6)
camlimages.core (version: 5.0.1)
camlimages.exif (version: 5.0.1)
camlimages.freetype (version: 5.0.1)
camlimages.gif (version: 5.0.1)
camlimages.graphics (version: 5.0.1)
camlimages.jpeg (version: 5.0.1)
camlimages.png (version: 5.0.1)
camlimages.tiff (version: 5.0.1)
camlimages.xpm (version: 5.0.1)
To figure out dependencies between those sublibraries, you can read the META file that describes the package, it is pretty readable and easy to locate with the following command
less $(ocamlfind query camlimages)/META
I suggest using camlimages.all
but if it complains you may select the subset that works for your setup.
来源:https://stackoverflow.com/questions/59060382/unbound-module-png-png-load