2

I'm using Getopt to parse commandline arguments in commands.ml. So the first line of commands.ml looks like this:

open Getopt

I can't seem to figure out how I compile commands.ml with this module. I've tried so many things and I always get the following error:

File "commands.ml", line 1, characters 5-11:
Error: Unbound module Getopt

I have added #require "Getopt" to my .ocamlinit file.

Thomas Vanhelden
  • 879
  • 8
  • 20

3 Answers3

3

You say you're using Getopt, "so" you have open Getopt in your code. But there's no direct connection there. It's more usual (and in my opinion usually better) to use modules without opening them.

The use of open only controls the names available in the containing module. It doesn't tell the compiler where to look for the opened modules.

There's no Getopt module in the standard OCaml library. The standard module for parsing command lines is named Arg. If you're using an external library, you need to use the -I flag to tell the compiler where to look for it.

The .ocamlinit file controls the behavior of the OCaml toplevel (the read-eval-print interpreter). It doesn't affect the behavior of compilers.

If you're using a building tool, there are probably easier ways to set things up. But you'll need to explain your build environment more carefully.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • I'm actually just looking for a command that successfully compiles commands.ml. I know I need to tell the compiler where to look for getopt, but I don't know how. I've tried commands using the `-I` flag bot like I said, nothing I've tried so far has been successful. What exactly do I have to but after the `-I` flag? I've installed `Getopt` using OPAM. – Thomas Vanhelden Jun 20 '15 at 09:26
1

The problem, that there're lots of answers to your question. Depending on what build system you chose, there will be different commands. And that is the reason, why we are asking. But it looks like, that you have no preference, so let me try to give you some answers.

With ocamlbuild

$ ocamlbuild -package getopt commands.native

With ocamlfind

$ ocamlfind ocamlopt -package getopt commands.ml -o commands.native

For more explanation read the following.

Personal advice: if you're unsure on what to choose, then use ocamlbuild.

Community
  • 1
  • 1
ivg
  • 34,431
  • 2
  • 35
  • 63
1

Use ocamlfind with your preferred compiler (I'm using ocamlopt below), like so:

ocamlfind ocamlopt -package getopt -linkpkg commands.ml -o commands

This will still fail if you don't have getopt installed. Getopt may be installed with opam:

opam install getopt

I would like to suggest that you use Arg instead. It's part of OCaml's standard library, and is generally pleasant to work with.

chrismamo1
  • 917
  • 1
  • 7
  • 15