现在的位置: 首页 > 综合 > 正文

[Erlang 0003] 编译Erlang程序

2012年07月14日 ⁄ 综合 ⁄ 共 3228字 ⁄ 字号 评论关闭
    

     如果语言学习也做一个"流失率统计",那么有多少人是在编译环节放弃的呢?按下F6就完成编译,F5就可以运行,这样的好日子不再了,你能接受么?
我们今天要说的就是Erlang的编译.
     在Windows环境中编译Erlang代码并没有想象中那么繁琐,[Erlang 0002] Erlang IDE  这篇文章里面介绍的一些工具比如Eclipse
和Netbeans都是可以通过简单的点击一下按钮就可以编译erlang文件的。下面重点介绍的是:不依赖IDE如何编译,以及在项目中如果使用
Emakefile来管理复杂的编译行为。
     
    不依赖IDE如何编译
     
    当前路径的问题?有文章介绍添加.erlang文件来解决这个问题的,实际上并没有这么复杂,在你的hello.erl文件所在文件夹新建一个cmd文件 ,
使用下面的命令行:
     
      "C:\Program Files (x86)\erl5.8.2\bin\erl.exe"  -name zen@192.168.1.199  -setcookie abc
      
     前面是erlang的安装路径,后面指定了启动的节点名和magic cookie,然后就可以直接使用c(hello)来编译hello.erl.
如果要添加代码路径,也比较简单,添加启动参数就可以:
 
 "C:\Program Files (x86)\erl5.8.2\bin\erl.exe"  -pa D:\code\  -pz E:\erlang
 
  上面的命令行就添加了两个路径,注意路径中斜线方向是和windows路径不同的;pa pz这两个参数等效下面的方法:
 
    code:add_patha(Path).
    code:add_pathz(Path).
 
这样我们就把代码路径的问题解决掉了,如果感兴趣你可以执行一下init:get_argument(home).看看这个路径在什么鬼地方。
 
 
 
 
 

项目中使用Emakefile定制编译
 
    项目实战中肯定不会把所有文件放在同一个目录里面,一般都会有include lib mod test ebin 等划分,甚至还会有更深的文件夹嵌套,
这时就需要通过定制Emakefile来管理编译行为,这里有两个选择:makefile或者Emakefile 后者是前者的一个简化版,实际配置也确实
更简单.
 
下面是一个典型样例:
{
[
'src/*',
'src/lib/*',
'src/mod/*',
'src/test/*',
]
,[
,{i, 'include'}
,{outdir, 'ebin'}
]
}.
 
上面结构一目了然:源码路径,头文件路径,输出目录;
 
把这个文件放在代码文件根目录,Eclipse和Netbeans的Erlang插件都会识别Emakefile.如果不使用这些IDE想完成这样复杂的编译,
在这里依然可以使用一个上面的cmd文件,启动之后可以通过执行make:all()进行编译,这时还可以通过
make:all添加其他的编译参数,比如 make:all([debug_info]). 详细见这里:http://www.erlang.org/doc/man/make.html
 
如果想启动命令行的时候就自动编译,可以直接在命令行添加:
 
 "C:\Program Files (x86)\erl5.8.2\bin\erl.exe"  -name zen@192.168.1.199  -setcookie abc -make
 
 
一位老外同行介绍Emakefile 的文章:
Smarter Erlang Programming with Emakefile Options and user_default
这篇文章穿针引线提到了不少和Erlang编译相关的资料,Enjoy it!
 
 
Emake file 样例
{"src/erlyweb/*", [debug_info, {outdir, "ebin"}, {i,"/opt/local/lib/yaws/include"}]}.
%{"src/erlyweb/*", [debug_info, {outdir, "ebin"}, {i,"/Users/yariv/yaws/include"}]}.
{"src/erlydb/*", [debug_info, {outdir, "ebin"}]}.
{"src/erlsql/*", [debug_info, {outdir, "ebin"}]}.
{"src/erltl/*", [debug_info, {outdir, "ebin"}]}.
{"src/smerl/*", [debug_info, {outdir, "ebin"}]}.
{"src/erlang-mysql-driver/*", [debug_info, {outdir, "ebin"}]}.
{"src/erlang-psql-driver/*", [debug_info, strict_record_tests, {outdir, "ebin"}]}.
{"src/lib/*", [debug_info, strict_record_tests, {outdir, "ebin"}]}.

 举例:

{
  [
     '*'
  ],
  [
    debug_info,
%   native,
%   {d,product},
%   {i, ["include"]},
%   {i,"include/mysql_statements"},
%   {i,"include/section"},
%   {i, "priv/src/mochiweb/include"},
    {outdir, "ebin"}
  ]
}.

再举例:

{
  [
    'priv/src/record_proplist/record_info_compiler_supplement.erl',
    'priv/src/record_proplist/record_info_transform.erl',
    'priv/src/record_proplist/record_info.erl'
  ],
  [
    %export_all
    debug_info,
    %{d, product},
    {i, ["include"]},
    {outdir, "ebin"},
    verbose,
    report
  ]
}.
{
  [
    'src/config_data/*',
    'src/mod/gen_player/gen_player.erl',
    'src/lib/item/*',
    'src/*',
    'src/**/*',
    'src/***/**/*',
    'src/****/***/**/*',
    'src/*****/****/***/**/*',
    'src/******/*****/****/***/**/*',
    'src/*******/******/*****/****/***/**/*',
    'priv/src/**/*',
    'priv/src/***/**/*',
    'test/*',
    'test/**/*',
    'test/***/**/*',
    'test/****/***/**/*'
  ],
  [
    %export_all
    debug_info,
%    {d, product},
%    {d, robot_rec},
    {i, ["include"]},
    {i, "include/section_ref"},
    {i, "include/section_business"},
    {i, "priv/src/mochiweb/include"},
    {outdir, "ebin"},
    verbose,
    warnings_as_errors,
    report
  ]
}.

 

 
 

-- 

It's not who you are underneath, it's what you do that defines you.

     
   

抱歉!评论已关闭.