Basic
Basic example to create add(a, b)
NIF. We will use nimler to automatically generate Elixir wrapper module, then test via elixir CLI.
import nimler
import nimler/codec
func add(env: ptr ErlNifEnv, a1: int, a2: int): int {.xnif.} =
return a1 + a2
exportNifs "Elixir.NumberAdder", [ add ]
-
Compile
nim c --app:lib -d:nimlerGenWrapper nif.nim
. Produceslibnif.so
andNumberAdder.ex
-
Run
elixir -r NumberAdder.ex -e "IO.inspect %{add: NumberAdder.add(1, 2)}"
Note:
exportNifs()
is a compile-time template that exports anErlNifEntry
to be loaded by Erlang or Elixir