1

我正在使用 NIF 在 Elixir 中集成第三方 C++ API 。这是共享的代码示例,当我像二进制文件一样运行它时工作正常。

class Client: public AbstractThirdPartyClient {
  public:
    int ConnectionSuccess(int status) {
       return status;
    }
};

int main () {
  Client *client = new Client;
  ThirdPartyControl *control = new ThirdPartyControl(client, "fully/qualified/path/to/some/config/file");
  return 0;
}

但是,当我尝试将 NIF 引入转换为动态库 (.so) 文件时,NIF 加载失败并出现 seg 错误,并且由于ThirdPartyControl对我隐藏了类实现,我很难弄清楚为什么会失败。


ERL_NIF_TERM setup(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
  return enif_make_atom(env, "ok");
}

static ErlNifFunc nif_funcs[] = {{"setup", 0, setup}};

int load(ErlNifEnv* caller_env, void** priv_data, ERL_NIF_TERM load_info) {
  try {
    Client *client = new Client;
    /* this line causes seg fault don't know why */
    ThirdPartyControl *control = new ThirdPartyControl(client, (char *)NULL, (char *)"fully/qualified/path/to/some/config/file");
    *priv_data = (void *)control;
  } catch (exception e) {
    return 1;
  }
  return 0;
}

int upgrade(ErlNifEnv* caller_env, void** priv_data, void **old_priv_data, ERL_NIF_TERM load_info) {
  return 0;
}

ERL_NIF_INIT(Elixir.Vendor.Middleware, nif_funcs, load, NULL, upgrade, NULL);
4

0 回答 0