Erlang Thursday – c:m/1

今天的Erlang Thursday继续研究c模块,讲的函数是 c:m/1.

c:m/1 入参是一个表示模块名字的原子,返回值是这个模块的信息。它打印出来的信息包括编译的日期、时间和编译选项,还有装载的目标(BEAM)文件以及这个模块中导出函数组成的列表。

我们来看看erlang中的string模块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
c:m(string).
% Module string compiled: Date: November 28 2014, Time: 06.47
% Compiler options: [{outdir,"/private/tmp/erlang-pY1Kv2/otp-OTP-17.3.4/lib/stdlib/src/../ebin"},
% {i,"/private/tmp/erlang-pY1Kv2/otp-OTP-17.3.4/lib/stdlib/src/../include"},
% {i,"/private/tmp/erlang-pY1Kv2/otp-OTP-17.3.4/lib/stdlib/src/../../kernel/include"},
% warnings_as_errors,debug_info]
% Object file: /usr/local/Cellar/erlang/17.3.4/lib/erlang/lib/stdlib-2.2/ebin/string.beam
% Exports:
% centre/2 rstr/2
% centre/3 span/2
% chars/3 str/2
% chars/2 strip/1
% chr/2 strip/2
% concat/2 strip/3
% copies/2 sub_string/2
% cspan/2 sub_string/3
% equal/2 sub_word/2
% join/2 sub_word/3
% left/2 substr/2
% left/3 substr/3
% len/1 to_float/1
% module_info/0 to_integer/1
% module_info/1 to_lower/1
% rchr/2 to_upper/1
% right/2 tokens/2
% right/3 words/1
% words/2
% ok

我们可以看到这个模块是2014年11月28日在我的机器上编译的,还看到 warnings_as_errors 和 debug_info 这两个编译选项打开了,还有beam文件的路径以及string模块所有导出的不同函数。

接着我们看看在erlang shell里编译的模块。

1
2
3
4
5
6
7
8
9
10
11
c(fizzbuzz).
% {ok,fizzbuzz}
c:m(fizzbuzz).
% Module fizzbuzz compiled: Date: August 5 2015, Time: 22.14
% Compiler options: []
% Object file: /Users/proctor/tmp/fizzbuzz.beam
% Exports:
% fizzbuzz/1
% module_info/0
% module_info/1
% ok

c:m(fizzubzz)的输出显示fizzbuzz被编译了,并且是从我的用户目录下的tmp目录装载的,同时还有一个导出函数fizzbuzz/1 和在每个模块都有的两个版本导出函数module_info。

今天讲的这个函数不是你每天都可能用的函数,不过了解它对你调试和检查你的erlang应用很有帮助。

原文链接: https://www.proctor-it.com/erlang-thursday-c-m-1/