SML-NJ,如何编译独立可执行文件

发布于 2024-10-18 03:07:57 字数 303 浏览 1 评论 0原文

我开始学习Standard ML,现在我尝试使用新泽西编译器的Standard ML。

现在我可以使用交互式循环,但是如何将源文件编译为独立的可执行文件?

例如,在 C 语言中,只需编写

$ gcc hello_world.c -o helloworld

然后运行 ​​helloworld 二进制文件。

我阅读了 SML NJ Compilation Manager 的文档,但它没有任何明确的示例。

另外,是否还有另一个 SML 编译器(允许独立的二进制创建)可用?

I start to learn Standard ML, and now I try to use Standard ML of New Jersey compiler.

Now I can use interactive loop, but how I can compile source file to standalone executable?

In C, for example, one can just write

$ gcc hello_world.c -o helloworld

and then run helloworld binary.

I read documentation for SML NJ Compilation Manager, but it don`t have any clear examples.

Also, is there another SML compiler (which allow standalone binary creating) available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-10-25 03:07:58

MosMLMLton 还可以创建独立的二进制文件。 MosML 通过 mosmlc 命令,MLton 通过 mlton 命令。

请注意,MLton 没有交互式循环,而是一个整体程序优化编译器。这基本上意味着编译需要相当长的时间,但反过来它会生成速度极快 SML 程序。

对于 SML/NJ,您可以使用 CM.mk_standalone 函数,但 CM 用户手册第 45 页中不建议这样做。相反,他们建议您使用 ml-build 命令。这将生成 SML/NJ 堆映像。堆映像必须使用 @SMLload 参数运行,或者您可以使用 heap2exec 程序,前提是您有受支持的系统。如果您不这样做,那么我建议您改用 MLton。

以下可用于生成有效的 SML/NJ 堆映像:

test.cm:

Group is 
        test.sml

        $/basis.cm

test.sml:

structure Test =
struct

fun main (prog_name, args) =
    let
      val _ = print ("Program name: " ^ prog_name ^ "\n")
      val _ = print "Arguments:\n"
      val _ = map (fn s => print ("\t" ^ s ^ "\n")) args
    in
      1
    end
end

要生成堆映像,您可以使用: ml-build test.cm Test.main test-image 然后通过 sml @SMLload test-image.XXXXX arg1 arg2 "this is one argument" 运行它,其中 XXXXX 是您的架构。

如果您在某个时候决定使用 MLton,那么您不需要任何 main 函数。它评估顶层的所有内容,因此您可以创建一个 main 函数并通过如下方式调用它:

fun main () = print "this is the main function\n"

val foo = 4

val _ = print ((Int.toString 4) ^ "\n")

val _ = main ()

然后您可以通过 mlton foo.sml 编译它,这将生成一个名为“foo”的可执行文件。当您运行它时,它将产生以下结果:

./foo 
4
this is the main function

请注意,这只是一个文件,当您有多个文件时,您将需要使用 MLB(ML Basis 文件),即 MLtons 项目文件,或者您可以使用 cm 文件,然后通过mltonprojectr.mlb编译它

Both MosML and MLton also have the posibility to create standalone binary files. MosML through mosmlc command and MLton through the mlton command.

Note that MLton doesn't have an interactive loop but is a whole-program optimising compiler. Which in basic means that it takes quite some time to compile but in turn it generates incredibly fast SML programs.

For SML/NJ you can use the CM.mk_standalone function, but this is not advised in the CM User Manual page 45. Instead they recommend that you use the ml-build command. This will generate a SML/NJ heap image. The heap image must be run with the @SMLload parameter, or you can use the heap2exec program, granted that you have a supported system. If you don't then I would suggest that you use MLton instead.

The following can be used to generate a valid SML/NJ heap image:

test.cm:

Group is 
        test.sml

        $/basis.cm

test.sml:

structure Test =
struct

fun main (prog_name, args) =
    let
      val _ = print ("Program name: " ^ prog_name ^ "\n")
      val _ = print "Arguments:\n"
      val _ = map (fn s => print ("\t" ^ s ^ "\n")) args
    in
      1
    end
end

And to generate the heap image you can use: ml-build test.cm Test.main test-image and then run it by sml @SMLload test-image.XXXXX arg1 arg2 "this is one argument" where XXXXX is your architecture.

If you decide to MLton at some point, then you don't need to have any main function. It evaluates everything at toplevel, so you can create a main function and have it called by something like this:

fun main () = print "this is the main function\n"

val foo = 4

val _ = print ((Int.toString 4) ^ "\n")

val _ = main ()

Then you can compile it by mlton foo.sml which will produce an executable named "foo". When you run it, it will produce this as result:

./foo 
4
this is the main function

Note that this is only one file, when you have multiple files you will either need to use MLB (ML Basis files) which is MLtons project files or you can use cm files and then compile it by mlton projectr.mlb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文