Compiling multiple Ocaml files

后端 未结 2 1874
醉酒成梦
醉酒成梦 2021-01-18 19:34

I am new to Ocaml and trying to write some small example application. I am using ocamlc version 3.11.2 under Linux Ubuntu 10.04. I want to compile two files:

相关标签:
2条回答
  • 2021-01-18 19:57

    It would be clearer if you showed the code that's not working. As Kristopher points out, though, the most likely problem is that you're not specifyig which module foo is in. You can specify the module explicitly, as A.foo. Or you can open A and just use the name foo.

    For a small example it doesn't matter, but for a big project you should be careful not to use open too freely. You want the freedom to use good names in your modules, and if you open too many of them, the good names can conflict with each other.

    0 讨论(0)
  • 2021-01-18 20:09

    First fix the unbound value issue, as explained by Jeffrey's answer. This is a comment about the commands you're using.

    Decomposing compilation in several steps is a good way to understand what's going on. If you want to write your own a.mli, most likely to hide some values of the module A, then your command ocaml -i -c a.ml > a.mli is a good way to get a first version of the this file and then edit it. But if you're not touching a.mli, then you don't need to generate it: you can also directly enter

    ocamlc -o foo a.ml b.ml
    

    which will produce a.cmo, b.cmo and the exectuable foo.

    (It will also generate a.cmi, which is the compiled version of a.mli, that you get by issuing ocamlc -c a.mli. Likewise it will also generate b.cmi).

    Note that order matters: you need to provide a.ml before b.ml on the command line. This way, when compiling b.ml, the compiler has already seen a.ml and knows where to find the module A.


    Some more comments:

    • You're right in your "As far as I understand" paragraph.
    • you don't really include a separate file, it's more like import in Python: the values of module A are available, but under the name A.foo. The contents of a.ml has not been copy-pasted into b.ml, rather, values of the module A, defined in a.ml and it's compiled version a.cmo have been accessed.
    • if you're using this module A in b.ml, you can pass any of the following on the command line before b.ml:
      • a.mli, which will get compiled into a.cmi
      • a.cmi if you've already compiled a.mli into a.cmi
      • a.ml or its compiled version a.cmo if you don't need to write your own a.mli, i.e. if the default interface of module A suits you. (This interface is simply every value of a.ml).
    0 讨论(0)
提交回复
热议问题