首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用rebar设置Erlang NIF项目?

如何使用rebar设置Erlang NIF项目?
EN

Stack Overflow用户
提问于 2013-06-26 03:49:30
回答 1查看 1.8K关注 0票数 3

我浏览了StackOverflow上的问题,并用谷歌搜索了一下在rebar中为包装C++库而设置一个基本的NIF项目的示例。

我曾经将GitHub上的库项目作为指南:

https://github.com/tuncer/re2

我的项目在这里:

https://github.com/project-z/emutton/

当我执行rebar compile && rebar eunit时,我在eunit测试中得到一个失败,因为它找不到emtn.so

代码语言:javascript
复制
$ rebar compile && rebar eunit 
==> emutton (compile)
==> emutton (eunit)
undefined
*** test module not found ***
**emtn

=ERROR REPORT==== 25-Jun-2013::12:21:55 ===
The on_load function for module emtn returned {error,
                                               {load_failed,
                                                "Failed to load NIF library: 'dlopen(/.../source/emutton/priv/emtn.so, 2): image not found'"}}
=======================================================
  Failed: 0.  Skipped: 0.  Passed: 0.
One or more tests were cancelled.
ERROR: One or more eunit tests failed.
ERROR: eunit failed while processing /.../source/emutton: rebar_abort

当我调用rebar compile时,它只生成一个驱动程序文件emtn_drv.so,而没有生成emtn.so

代码语言:javascript
复制
$ tree priv 
priv
└── emtn_drv.so

0 directories, 1 file

我在c_src/build_deps.sh中有一个echo语句,当我调用rebar clean时看不到输出。似乎我的pre_hookrebar.config中的post_hook被完全忽略了:

代码语言:javascript
复制
{pre_hooks, [{compile, "c_src/build_deps.sh"}]}.
{post_hooks, [{clean, "c_src/build_deps.sh clean"}]}.

rebar未显示输出的示例:

代码语言:javascript
复制
$ rebar compile 
==> emutton (compile)
$ rebar clean 
==> emutton (clean)

因为我已经克隆了tuncer的RE2绑定项目,并且当我执行rebar compile时,可以看到他的build_deps.sh脚本的输出。我的权限与他的权限一致:

代码语言:javascript
复制
-rwxr-xr-x  1 ajl  staff   891B Jun 25 12:30 c_src/build_deps.sh

你知道我错过了什么吗?我相信rebar配置正确,可以调用脚本并进行编译。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-26 05:08:10

您的问题是rebar.config中的行

https://github.com/project-z/emutton/blob/master/rebar.config%20#L1

与您尝试加载的内容不匹配

https://github.com/project-z/emutton/blob/master/src/emtn.erl#L25

您应该将rebar.config更改为

代码语言:javascript
复制
{port_specs, [{"priv/emtn.so",["c_src/emtn_nif.c"]}]}.

并将emtn.erl更改为

代码语言:javascript
复制
erlang:load_nif(filename:join(PrivDir, "emtn"), 0).  % ?MODULE is an atom, I believe you need a string

或将emtn.erl更改为

代码语言:javascript
复制
erlang:load_nif(filename:join(PrivDir, "emtn_drv"), 0).
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17306235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档