Erlang 编译并运行程序

在 erl shell 中编译运行

ERLANG程序设计-code\code>erl
Eshell V5.8.2 (abort with ^G)
1> c(hello).
{ok,hello}
2> hello:start().
Hello world
ok
3>

在命令提示符下编译运行

ERLANG程序设计-code\code>erlc hello.erl
ERLANG程序设计-code\code>erl -noshell -s hello start -s init stop
Hello world

ERLANG程序设计-code\code>

快速脚本

-eval 参数可以在 os 命令行中执行任意的erlang函数,例如:

$ erl -eval ‘io:format(“Hello world~n”).’ -noshell -s init stop
Hello world
$ erl -eval ‘io:format(“Memory:~p~n”,[erlang:memory(total)]).’ -noshell -s init stop
Memory:2854918
$
上面这个命令在windows下无输出,原因是单引号的问题,将单引号换成双引号就可以了,可能是windows直接将单引号传递给了erl,被erl当作了原子了。
windows下需要执行下面的命令
D:\My Dropbox\Erlang\ERLANG程序设计-code\code>erl -eval “io:format(\”Hello world
\”).” -noshell -s init stop
Hello world
D:\My Dropbox\Erlang\ERLANG程序设计-code\code>
erlc hello.erl 编译文件hello.erl,生成名为hello.beam的目标代码文件。
-noshell 表示启动erlang,但是关闭shell,因此不会看到erlang提示信息。
-s hello start 表示运行函数 hello:start(). ,-s 执行模板函数时模板必须已经编译过,否则出现下面的错误:
ERLANG程序设计-code\code>erl  -noshell -s hello start -s init stop
{“init terminating in do_boot”,{undef,[{hello,start,[]},{init,start_it,1},{init,start_em,1}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
-s init stop 表示运行 init:stop() 函数。当有多个 -s 参数时,会按顺序对每个-s命令有一个apply语句进行解释运行,一旦一个运行完毕,则继续执行下一个命令。
  WordPress › 错误