如何编译多个SML文件?
How to compile multiple SML files?
如何在 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.
那么,如何处理多个文件呢?
您可以在 Moscow ML Owner’s Manual 中阅读更多内容,但在您的特定情况下,以下命令甚至无需在源代码中使用 use
即可工作:
mosmlc -toplevel file1.sml file2.sml
使用结构模式
当您想将代码组织成结构时,可以使用mosmlc
的-structure
标志。例如,给定以下文件:
Hello.sml
structure Hello =
struct
val hello = "Hello"
end
World.sml
structure World =
struct
structure H = Hello
val world = H.hello ^ ", World!"
end
main.sml
fun main () =
print (World.world ^ "\n")
val _ = main ()
您现在可以获得一个名为 main
的可执行文件,如下所示:
mosmlc -structure Hello.sml World.sml -toplevel main.sml -o main
然后运行它:
$ ./main
Hello, World!
结构模式要求文件名和包含的结构一致,就像在Java 类中一样,文件必须具有相同的名称。您还可以使用包含签名的 .sig
个文件。
如何在 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.
那么,如何处理多个文件呢?
您可以在 Moscow ML Owner’s Manual 中阅读更多内容,但在您的特定情况下,以下命令甚至无需在源代码中使用 use
即可工作:
mosmlc -toplevel file1.sml file2.sml
使用结构模式
当您想将代码组织成结构时,可以使用mosmlc
的-structure
标志。例如,给定以下文件:
Hello.sml
structure Hello =
struct
val hello = "Hello"
end
World.sml
structure World =
struct
structure H = Hello
val world = H.hello ^ ", World!"
end
main.sml
fun main () =
print (World.world ^ "\n")
val _ = main ()
您现在可以获得一个名为 main
的可执行文件,如下所示:
mosmlc -structure Hello.sml World.sml -toplevel main.sml -o main
然后运行它:
$ ./main
Hello, World!
结构模式要求文件名和包含的结构一致,就像在Java 类中一样,文件必须具有相同的名称。您还可以使用包含签名的 .sig
个文件。