Separate compilation of OCaml modules

后端 未结 1 654
难免孤独
难免孤独 2020-12-19 19:16

I have read this question and others, but my compile problem is unsolved.

I am testing separate compilation with these files:

testmoda.ml

m         


        
相关标签:
1条回答
  • 2020-12-19 19:58

    OCaml gives you a module for free at the top level of each source file. So your first module is actually named Testmoda.Testmoda, the function is named Testmoda.Testmoda.greeter, and so on. Things will work better if your files just contain the function definitions.

    As a side comment, if you're going to use the interface generated by ocamlc -i, you really don't need mli files. The interface in the absence of an mli file is the same as the one generated by ocamlc -i. If you don't want the default interface, using ocamlc -i gives a good starting point for your mli file. But for a simple example like this, it just makes things look a lot more complicated than they really are (IMHO).

    If you modify your files as I describe (remove extra module declarations), you can compile and run from scratch as follows:

    $ ls
    testmod.ml  testmoda.ml testmodb.ml
    $ cat testmoda.ml
    let greeter () = print_endline "greetings from module a"
    $ cat testmodb.ml
    let dogreet () = print_endline "Modul B:"; Testmoda.greeter ()
    $ ocamlc -o testmod testmoda.ml testmodb.ml testmod.ml
    $ ./testmod
    Calling modules now...
    greetings from module a
    Modul B:
    greetings from module a
    End.
    

    If you have already compiled a file (with ocamlc -c file.ml) you can replace .ml with .cmo in the above command. This works even if all the filenames are .cmo files; in that case ocamlc just links them together for you.

    0 讨论(0)
提交回复
热议问题