Erlang, is it possible to reload or update the nif library without restarting the shell?

I have an nif library and every time I recompile it, I have to restart the shell in order to reload or update this library.

Here is my erlang code:

-module(q4).
-export([init/0]).

-on_load(init/0).


init() ->
    erlang:load_nif("./q4_nif", reload).

Every time I compile the erlang module, this error occurs:

`The on_load function for module q4 returned {error,
                                         {upgrade,
                                          "Upgrade not supported by this NIF library."}}`

and when I call the function init/0, this error occurs: {error,{reload,"Reload not supported by this NIF library."}}

Do I need to fix this problem at all and load the new nif library without restarting the shell?

+4
source share
2 answers

As indicated in the error message, you need to provide a function upgradein your NIF that you specify when calling ERL_NIF_INIT:

ERL_NIF_INIT(MODULE, ErlNifFunc funcs[], load, reload, upgrade, unload)

erl_nif.

+4

, delete NIF. erlang.

force_upgrade_module(Mod) ->
  true == code:purge(Mod),
  true == code:delete(Mod),
  code:purge(Mod),
  code:delete(Mod),
  {module,Mod} == code:load(Mod).

upgrade ERL_NIF_INIT , . , macOS: priv/{{module}}.so load_nif stub erlang, NIF.so.

+1

Source: https://habr.com/ru/post/1613751/


All Articles