<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; erl_nif， on_load</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/erl_nif%ef%bc%8c-on_load/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>erl_nif Erlang的ffi 扩展erlang的另外一种方法</title>
		<link>http://blog.yufeng.info/archives/163</link>
		<comments>http://blog.yufeng.info/archives/163#comments</comments>
		<pubDate>Wed, 25 Nov 2009 17:04:24 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[erl_nif， on_load]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=163</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: erl_nif Erlang的ffi 扩展erlang的另外一种方法 我们知道扩展erl有2种方法， driver和port. 这2个方法效率都低，因为都要经过 port机制，对于简单的模块，这个开销有时候是不可接受的。这时候nif来救助了。今天发布的R13B03已经支持了，虽然是实验性质的。 erl_nif的代表API functions for an Erlang NIF library。 参考文档： erl_nif.html 和 erlang.html#erlang:load_nif-2 以及 reference_manual/code_loading.html#id2278318 。 我们来用nif写个最简单的hello, 来展现nif的威力和简单性。 不啰嗦，直接上代码： 编译： 运行： 现在重新修改下 niftest.erl 把on_load的注释去掉 综述： nbif很简单，而且高效。 Post Footer automatically generated by wp-posturl plugin for wordpress.]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/163">erl_nif Erlang的ffi 扩展erlang的另外一种方法</a></p>
</div>
<p>我们知道扩展erl有2种方法， driver和port. 这2个方法效率都低，因为都要经过 port机制，对于简单的模块，这个开销有时候是不可接受的。这时候nif来救助了。今天发布的R13B03已经支持了，虽然是实验性质的。 erl_nif的代表API functions for an Erlang NIF library。 参考文档：<br />
erl_nif.html 和 erlang.html#erlang:load_nif-2 以及 reference_manual/code_loading.html#id2278318 。</p>
<p>我们来用nif写个最简单的hello, 来展现nif的威力和简单性。<br />
不啰嗦，直接上代码：</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~/niftest# cat niftest.c
</pre>
<pre class="brush: cpp; title: ; notranslate">
/* niftest.c */
#include &quot;erl_nif.h&quot;
static ERL_NIF_TERM hello(ErlNifEnv* env)
{
return enif_make_string(env, &quot;Hello world!&quot;);
}
static ErlNifFunc nif_funcs[] =
{
{&quot;hello&quot;, 0, hello}
};
ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~/niftest# cat niftest.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(niftest).
%-on_load(init/0).
-export([init/0, hello/0]).
init() -&amp;gt;
ok=erlang:load_nif(&quot;./niftest&quot;, 0), true.
hello() -&amp;gt;
&quot;NIF library not loaded&quot;.
</pre>
<p>编译：</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~/niftest# gcc -fPIC -shared -o niftest.so niftest.c -I   /usr/local/lib/erlang/usr/include  #你的erl_nif.h路径
</pre>
<p>运行：</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~/niftest# erl
Erlang R13B03 (erts-5.7.4) 1 [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]&lt;/code&gt;

Eshell V5.7.4  (abort with ^G)
1&gt; c(niftest).
{ok,niftest}
2&gt; niftest:hello().
&quot;NIF library not loaded&quot;
3&gt; niftest:init().
ok
4&gt; niftest:hello().
&quot;Hello world!&quot;
5&gt;
</pre>
<p>现在重新修改下 niftest.erl 把on_load的注释去掉</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~/niftest# erl
Erlang R13B03 (erts-5.7.4) 1 [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]&lt;/code&gt;

Eshell V5.7.4  (abort with ^G)
1&amp;gt; c(niftest).
{ok,niftest}
2&amp;gt; niftest:hello().
&quot;Hello world!&quot;
3&amp;gt;
</pre>
<p>综述： nbif很简单，而且高效。
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/163/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

