Tutorial 1: First Steps
A simple Example
Let’s say you want to create a NIF module from the following example C file fib.c
:
Nifty needs a header file to create a NIF module, so let’s write one and store it under fib.h
Now we can create a NIF module, using Nifty:
Now let’s try to load the module:
The loader reports, that it cannot find the function fib. What happened? When we generated the NIF, we forgot to tell
Nifty that it should also compile fib.c
:
The third arguments specifies the compile options which are equivalent to the options that you can set in a rebar.config
.
To add a source file or compile options to your NIF module, you have to use the target
{".*", "$NIF", [<source files>], <optional options>}
in the port_specs option. Fortunatley, Nifty comes with
some utility functions, that help creating such a configuration. We want to add a source file to an empty configuration. an empty
configuration is []
and we can add the source fib.c
with nifty_utils:add_sources
.
When you leave the interactive interpreter, you will see that Nifty created a complete Erlang project:
fib
├── c_src
│ └── fib_nif.c
├── ebin
│ └── fib.app
├── include
│ └── fib.hrl
├── priv
├── rebar.config
└── src
├── fib_remote.erl
└── fib.erl
Tutorial Files | Next Tutorial |
---|