SML-NJ, how to compile standalone executable

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

Leave a Comment