如何编译多个SML文件?

时间:2016-04-29 21:08:34

标签: sml mosml

如何在Standard-ML中编译多个文件?我有2个文件。

file1.sml

(* file1.sml *)
datatype fruit = Orange | Apple | None

file2.sml

(* file2.sml *)
datatype composite = Null | Some of fruit

因为您可以看到file2.sml正在使用file1.sml中的内容。我怎样才能编译这个东西?

我正在使用mosmlc.exe并在编译mosmlc file2.sml时(和this question一样):

(* file2.sml *)
use "file1.sml";
datatype composite = Null | Some of fruit

我明白了:

! use "file1.sml";
! ^^^
! Syntax error.

那么,如何处理多个文件?

1 个答案:

答案 0 :(得分:5)

您可以在Moscow ML Owner’s Manual中阅读更多内容,但在您的特定情况下,以下命令无需在源代码中使用use即可运行:

mosmlc -toplevel file1.sml file2.sml

使用结构模式

如果要将代码整理到结构中,可以使用-structure的{​​{1}}标记。例如,给定以下文件:

Hello.sml

mosmlc

World.sml

structure Hello =
  struct
    val hello = "Hello"
  end

main.sml

structure World =
  struct
    structure H = Hello

    val world = H.hello ^ ", World!"
  end

您现在可以获得名为fun main () = print (World.world ^ "\n") val _ = main () 的可执行文件,如下所示:

main

然后运行它:

mosmlc -structure Hello.sml World.sml -toplevel main.sml -o main

结构模式要求您将文件名和包含的结构重合,就像在Java类中一样,文件必须具有相同的名称。您还可以使用包含签名的$ ./main Hello, World! 个文件。

相关问题