7

I have a nif library and every time i recompile it, I must restart the shell to reload or upgrade 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 init/0 function, this error occurs: {error,{reload,"Reload not supported by this NIF library."}}

Is there anyway to fix this problem and load new nif library without restarting the shell?

4

3 回答 3

6

如错误消息所示,您需要upgrade在 NIF 中提供一个函数,您在调用时指定该函数ERL_NIF_INIT

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

升级功能记录在erl_nif手册页中。

于 2015-10-30T00:36:10.613 回答
2

更新找到根本原因

在热重载 NIF 之前,它似乎delete必须调用两次。这似乎是一个 erlang 错误。

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

即使在 中实现upgradeERL_NIF_INIT仍然存在一些可能与平台无关的问题。例如,在 macOS 上:随意删除 erlang 存根模块中的priv/{{module}}.soafter call load_nif,它只会继续默默地成功,而不是真正重新加载 NIF .so。

于 2017-05-02T05:40:47.743 回答
0

@steve-vinoski 在这种情况下应该load, reload,upgradeunload

ERL_NIF_INIT(MODULE, ErlNifFunc funcs[], load, reload, upgrade, unload)
于 2021-09-02T12:28:06.863 回答