Hi I'm trying to get the Hello World example for Erlang NIF (Native Implemented Function) shown here http://www.erlang.org/doc/man/erl_nif.html to work from Elixir on OSX 64bit.
First I create the C-code:
/* niftest.c */
#include "erl_nif.h"
static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1);
}
static ErlNifFunc nif_funcs[] =
{
{"hello", 0, hello}
};
ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)
Then I successfully compile it using gcc
for 64 bit architecture as suggested here
Erlang NIF Test -- OS X Lion
gcc -undefined dynamic_lookup -dynamiclib niftest.c -o niftest.so -I /usr/local/Cellar/erlang/R14B02/lib/erlang/usr/include
which produces the necessary file niftest.so
that I should be able to interface-into from Erlang/Elixir. My Elixir (niftest.ex) looks like this (inspired by a more complex example reported here):
defmodule Niftest do
@onload :init
def init() do
:erlang.load_nif("./niftest", 0)
:ok
end
def hello() do
"NIF library not loaded"
end
end
Now with niftest.so and niftest.ex in the same directory I fire up elixir using iex
and type in Niftest.hello
and all I get back is: "NIF library not loaded"
Am I missing a vital step? - please help!