Home

Awesome

Travis Erlang Versions Clang Versions Last Commit

Nifty - Erlang NIF Wrapper Generator

Nifty is an interface generator that allows the use of C modules from Erlang.

Web page: [http://parapluu.github.io/nifty/]

A Simple Example

Let's say we have two C files mylib.h and mylib.c which we want to use in our Erlang application:

/* mylib.h */
extern int fib(int n);
/* mylib.c */
int
fib(int n) {
  if (n <= 2) {
    return 1;
  } else {
    return fib(n-1) + fib(n-2);
  }
}

We can generate a NIF interface and use it from Erlang with the following command:

nifty:compile("mylib.h", mylib, nifty_utils:add_sources(["mylib.c"], [])).
5 = mylib:fib(5).

nifty:compile/3 gets as its first argument a header or interface file and tries to generate an interface for all specified functions. The second argument specifies the Erlang module the interface is about to use and the third argument is used for additional options. These options are compatible with the ones rebar uses to compile NIF modules. In fact, Nifty uses rebar to compile the generated interface.

Installation

After successfully cloning enter the following commands

make

and include Nifty in your ERL_LIBS path.

Unit Tests

Run the following command to check that everything works correct:

make tests

Dependencies

Base Types

C TypesErlang TypesNifty Type Name
signed int or intinteger()nifty.int
unsigned intinteger()nifty.unsigned int
charinteger()nifty.char
shortinteger()nifty.short
longinteger()nifty.long
long longinteger()nifty.long long
floatfloat()nifty.float
doublefloat()nifty.double
<type> *{integer(), string()}<module>.<type> *

Known Limitations

<!-- Links (alphabetically) --> <!-- Badges (alphabetically) -->