:erlang.load_nif/2 finds shared library file inside original project but can't find it if the project gets imported

I've build a small elixir application that uses NIF functions to execute some c++ code. The nifs are loaded via:

def load_nifs do
    :erlang.load_nif('<relative_path_to_lib>/<lib_name>', 0)

and this works fine. Now I want to integrate this app into another project. The problem now is that load_nif throws: Failed to load NIF library: '<relative_path_to_lib>/<lib_name>.so: cannot open shared object file: No such file or directory'' although nothing has changed. I checked the deps folder and the shared library files are exactly where they are supposed to be, so the dependency seems to be loaded correctly. I also tried putting the .so files into the same folder as the module that calls load_nif (and omit <relative_path_to_lib>/) as well as providing an absolute path, all to no avail.

Any help is appreciated, Cheers.

Relevant info regarding my system:

  • OS: Ubuntu 22.04
  • Elixir version: Elixir 1.13.0 (compiled with Erlang/OTP 24)


The issue does not seem to be that files are located at the wrong place, as it finds the files during the first test run after compilation. However, the error occurs when I repeat the run. It seems that the error message is wrong, since no files are deleted during the test.

If I repeat the function within one test multiple times there's no problem, so the issue is not created because the NIF function is executed multiple times, but because the test that contains the function is repeated multiple times.


  • Solution:

    I still have no idea what causes this behavior but after putting the .so files into a priv directory and accessing them via

    :erlang.load_nif(:code.priv_dir(:<app_name>), 0)

    the tests pass.