本文最后更新于:June 30, 2023 pm

概要:Erlang程序设计基础知识讲解

申明:此系列文章来源于【https://gitee.com/yujian1018/erlang.git】,经过本人搬运整理,并非原作,希望作者收到我的邮件以后联系本人。如有侵权会及时删除。


1. ubuntu安装

sudo apt-get install make gcc openssl libssl-dev libncurses-dev libwxgtk3.0-dev m4 glibc-devel build autoconf

./configure --enable-native-libs

make && make install

2. CentOS安装

yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel

描述

lib库 功能
libncurses-dev 系统的必备库,基础库
build-essential 提供编译程序必须软件包的列表信息
libwxgtk3.0-dev 图形库
libssl-dev 加密库
m4 native 模式 hipe
hipe erlang-base-hipe
fop Apache FOP print formatter (requires Java).
xsltproc A command line XSLT processor
Xmllint libxml2-utils
libpam0g-dev PAM开发文件
unixodbc-dev odbc支持mysql
freeglut3-dev 允许用户在众多的平台的创建和管理窗口中OpenGL容器,以及相关的鼠标、键盘和游戏杆功能
openjdk-8-jdk jdk

3. 设置环境变量

在/etc/profile的最下面添加

export ERL_HOME=/usr/local/erlang
export PATH=$ERL_HOME/bin:$PATH  #PS:注意是冒号隔开而不是分号
source /etc/profile
ln /usr/local/erlang/lib/erlang/bin/erl /usr/sbin/erl

4. 安装问题

描述 解决方案
configure: error: No curses library functions found yum list|grep ncurses
yum -y install ncurses-devel
jinterface : No Java compiler found
odbc : ODBC library - link check failed yum install unixODBC-devel
crypto : No usable OpenSSL found yum install openssl-devel
ssh : No usable OpenSSL found yum install openssl-devel
ssl : No usable OpenSSL found yum install openssl-devel
No C++ compiler found yum install gcc-c++

5. 开发工具

6. 在Eshell中编译运行

1. ErlangShell

$erl
Erlang (BEAM) emulator version 5.5.1 [source] [async一threads:0] [hipe]
Eshell V5.5.1  (abort with }G)
1> c(hello).
{ok,hello}
2> hello:start().
Hello world
ok
3> c("test.erl", [to_core]).   
4> c(test).   %编译
{ok,test}   
5> erts_debug:df(test).   %生成opcode 
ok

2. 编译

$ erlc hello.erl
$ erlc -h  
$ erlc -o ebin -I include/ src/*.erl  
$ erlc +"'S'" mod.erl %产生自解码 .S汇编代码 
$ erlc +"'P'" mod.erl  
$ erlc +"'E'" mod.erl  
$ erl -noshell -s hello start -s init stop
Hello world

$erl -eval 'io:format("Memory:~p~n", [erlang:memory(total)]).' -s init stop

3. MakeFile

{'file1',[debug_info,{i,"../foo"}]}. {'*',[debug_info]}.
{"src/erlyweb/*",  [debug_info, {outdir, "ebin"}, {i,"/opt/local/lib/yaws/include"}]}.  
{"src/erlydb/*", [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"} ] }.

make:

make:all().
make:all(Options). %根据当前目录下的Emakefile配置文件进行编译
$ erl -make %自动查找当前目录下的Emakefile

4. 脚本运行

  1. Linux shell
    #!/bin/sh
    erl -noshell -pa /home/joe/code -s hello start -s init stop
  2. windows bat
    "C:\Program Files\erl5.5.3\bin\erl.exe" -pa joe/code -noshell -s hello start -s init stop

5. escript运行

vim factorial.escript

#!/usr/bin/env escript
main([A]) ->    
	I = list_to_integer(A),
    F = fac(I),
    io:format("factorial~w=~w~n", [I, F]).
fac(0) -> 1;
fac(N) -> N * fac(N - 1).

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

什么样的企业算是合格 Previous
1.2基础操作 Next