erlang.mk
- 根据erlang.mk官网 Chapter 2. Getting started,执行 2.5. Getting started with OTP releases时,运行make时报错,错误信息如下:
lghdeMacBook:test lgh$ make
DEPEND test.d
APP test
=ERROR REPORT==== 5-Jul-2018::16:28:25 ===
Loading of /Users/lgh/lgh/work/test/.erlang.mk/relx/relx/ebin/relx.beam failed: badfile
=ERROR REPORT==== 5-Jul-2018::16:28:25 ===
beam/beam_load.c(1161): Error loading module relx:
mandatory chunk of type 'Atom' not found
escript: exception error: undefined function relx:main/1
in function escript:run/2 (escript.erl, line 752)
in call from escript:start/1 (escript.erl, line 276)
in call from init:start_it/1
in call from init:start_em/1
make: *** [erlang.mk:6746: relx-rel] Error 127
原因是因为我之前安装的erlang版本有好几个, 有erlang 15B、erlang17.0和erlang21.0等,删掉erlang 15B和erlang17.0,再make就可以了。
用erlang.mk构建项目:
1、建文件夹 mkdir simple_chat_sys
2、进入文件夹,curl -O https://erlang.mk/erlang.mk 下载erlang.mk
3、make -f erlang.mk bootstrap-lib bootstrap-rel 创建项目
4、修改Makefile文件PROJECT = simple_chat_sys PROJECT_DESCRIPTION = A simple_chat_sys Code with by lgh PROJECT_VERSION = 0.1.0 DEPS = ranch ##依赖ranch include erlang.mk
5、创建relx.conf文件,并加入以下代码:erlang.mk会检测工作目录下是否存在relx.conf文件,生成发布(即生成_rel)所需的文件
{release, {chat_sys, "1"}, [simple_chat_sys, sasl, runtime_tools]}. {extended_start_script, true}. {sys_config, "rel/sys.config"}. {vm_args, "rel/vm.args"}.
6、在src目录下创建文件simple_chat_sys.src.app,内容如下:
{application, 'simple_chat_sys', [ {description, "A simple_chat_sys Code with by lgh"}, {vsn, "0.1.0"}, {modules, ['server_start']}, {registered, []}, {applications, [kernel,stdlib,ranch,compiler]}, ## {mod, {server_start, []}}, {env, []} ]}.
7、最后再make编译项目, 会在ebin文件夹下生成simple_chat_sys.app文件,内容会跟第6步文件的一样。
8、可以执行make run
或者_rel/chat_sys/bin/chat_sys console
启动服务,服务的入口是simple_chat_sys.app配置文件的mod参数的方法server_start,并且该方法必须返回{ok, Pid}.
课外知识点
$ make SFX=1
This will create a self-extracting archive in $RELX_OUTPUT_DIR/\.run. For example if the release is named hello_world and $RELX_OUTPUT_DIR is the default, the file will be located at _rel/hello_world.run.
更多请参考:https://erlang.mk/guide/
erlang.mk源码:https://github.com/ninenines/erlang.mk
ranch源码:https://github.com/ninenines/ranch