[erlang-questions] Drive by mention of new features without explaination

Zoltan Lajos Kis kiszl@REDACTED
Mon Dec 7 20:48:57 CET 2009


Jarrod Roberson wrote:
>> http://www.erlang.org/doc/reference_manual/code_loading.html#id2278452
>>     
>
> thanks Bob for the link, following those instructions I got this far:
>
> // niftest.c
> #include "erl_nif.h"
> static ERL_NIF_TERM hello(ErlNifEnv* env)
> {
>     return enif_make_string(env, "Hello world!");
> }
> static ErlNifFunc nif_funcs[] =
> {
>     {"hello", 0, hello}
> };
> ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)
>
> ========================
>
> %% niftest.erl
> -module(niftest).
> -export([hello/0]).
> -on_load(run_me/0).
>
> run_me() ->
>       erlang:load_nif("./niftest", 0).
> hello() ->
>       "NIF library not loaded".
>
> ========================
>
> [jrod@REDACTED] [~]
> make clean all
> rm -f *.o *.beam niftest.so
> cc -arch x86_64 -O3 -fPIC -bundle -flat_namespace -undefined suppress
> -fno-common -Wall
> -I/Users/jrod/Downloads/otp_src_R13B03//erts/emulator/beam/   -c -o
> niftest.o niftest.c
> gcc -o niftest.so niftest.o -arch x86_64 -O3 -fPIC -bundle
> -flat_namespace -undefined suppress -fno-common -Wall
> erlc niftest.erl
> [jrod@REDACTED] [~]
> erl
> Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:2:2] [rq:2]
> [async-threads:0] [kernel-poll:false]
>
> Eshell V5.7.4  (abort with ^G)
> 1> niftest:hello().
> ** exception error: undefined function niftest:hello/0
> 2>
>
> I tried exporting the run_me/0 function as well with the same results?
> If I take out the -on_load(run_me/0). and add run_me/0 to the export it works.
> Any ideas what I am missing here?
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   
I also overlooked this a few days ago :).

The on_load function expects 'true' to be returned, whereas 
erlang:load_nif returns with 'ok' (which is definitely not 'true'). This 
will result in your module being unloaded.
Solution:

run_me() ->
      erlang:load_nif("./niftest", 0),
      true.



Regards,
Zoltan.


More information about the erlang-questions mailing list