[erlang] 얼랭 파일 컴파일 후 실행하기
얼랭 파일 컴파일 하기
$ cat > helloworld.erl
% Hello World
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello world, samuel!\n").
컴파일은 erlc를 사용한다. 컴파일하면 helloword.beam 파일이 생성됨을 확인할 수 있다.
$ ls -al
total 8
drwxr-xr-x 3 Samuel wheel 102 11 7 00:00 .
drwxr-xr-x 5 Samuel wheel 170 11 7 00:00 ..
-rw-r--r-- 1 Samuel wheel 109 11 6 23:59 helloworld.erl
$ erlc helloworld.erl
$ ls -al
total 16
drwxr-xr-x 4 Samuel wheel 136 11 7 00:00 .
drwxr-xr-x 5 Samuel wheel 170 11 7 00:00 ..
-rw-r--r-- 1 Samuel wheel 596 11 7 00:00 helloworld.beam
-rw-r--r-- 1 Samuel wheel 109 11 6 23:59 helloworld.erl
beam 확장자 파일은 얼랭 컴파일 파일이다. java처럼 가상머신에서 동작하는 바이너리 파일이다.
http://www.erlang.se/~bjorn/beam_file_format.html
얼랭 쉘에서 결과를 실행한다. -s옵션을 주어 모듈이름과 함수 이름을 추가한다.
$ erl -s helloworld start
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Hello world, samuel!
Eshell V7.1 (abort with ^G)
1>
단순하게 'erl -s 모듈이름 함수이름'을 실행 후, 얼랭 쉘에 남아있게 된다. -s 옵션을 더 주어 얼랭 커맨드를 정상적으로 모듈을 실행하고 종료하도록 -s init stop을 준다. (만약 -s init 만 주고 실행하면, 비정상적 종료라 여기고 core dump 하니. 같이 넣어주는 것이 좋은 것 같다.)
$ erl -s helloworld start -s init stop
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Hello world, samuel!
Eshell V7.1 (abort with ^G)
1>
$
얼랭 쉘 내용이 보기 싫다면, -noshell을 추가한다.
$ erl -noshell -s helloworld start -s init stop
Hello world, samuel!
정상적으로 실행을 마무리했다.
이번에는 얼랭 쉘을 미리 실행하고 컴파일된 helloworld 얼랭 파일을 읽어본다. c() 함수로 컴파일된 얼랭 파일 모듈을 읽고, 모듈의 함수를 실행하면 끝이다.
$ erl
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G)
1> c(helloworld).
{ok,helloworld}
2> helloworld:start().
Hello world, samuel!
ok