Building C/C++
🚧
No port compiler
In Rebar3 it is required to have either a Makefile or other instructions for building your C/C++ code outside of Rebar3 itself.
Using the Makefile Template
We’ll start by making a new lib named test_nif
and then using the cmake
template from the root of the test_nif
project.
In test_nif
's rebar.config
, add the pre_hooks line so that make
is called when compile
is run. Furthermore, add the post_hooks
entry for cleaning up the built C object files.
The Makefile
written by rebar3 new cmake
is a GNU Makefile, which means you will need to have GNU Make installed on the system. In the example, we provide a handler for the FreeBSD operating system, which assumes GNU Make is called gmake
.
Below is a NIF which has a function repeat
that will take a pid
and an Erlang term to send to that pid
.
Modify test_nif.erl
to load the test_nif
shared library from priv
and export repeat/2
.
Note that the error()
function will cause Dializer errors. Using erlang:nif_error/1
will not, and is preferred here.
Run rebar3 shell
and give the NIF a try.