jordan0day

jordan0day

NIF - Any way to set LD_LIBRARY_PATH during compilation?

I’m in the process of writing a NIF library application which in turn depends on a vendor’s shared lib (which has it’s own set of dependencies as well). My hope is to just keep all the various .so’s contained in /priv.

I’m using elixir_make to build my NIF’s code into it’s .so, which is also spit out into /priv by the makefile.

If I just run make, my /priv folder contains vendor.so, vendor_dep1.so, my_app_nif.so, which are all the files I would expect to be present.

I can successfully compile the elixir side of my application with: LD_LIBRARY_PATH=./priv mix compile, but I was wondering if there was a good way to set the LD_LIBRARY_PATH variable from within the project, so another user can just mix compile and it just works? Otherwise mix compile fails when compiling my nif module due to the @on_load bits normally set when writing nifs:

defmodule MyApp.Nif do
  @on_load :load_nif

  # code uglified to keep the post short
  def load_nif do
    nif_path = :my_app |> :code.priv_dir() |> List.to_string() |> Path.join("my_app_nif") |> String.to_charlist()
    :erlang.load_nif(nif_path, 0) # Fails with 'vendor.so' cannot open shared object file: No such file or directory
  end
end

Unfortunately :erlang.load_nif/2 doesn’t appear to have any options for specify additional library search paths. I’d be interested in hearing any suggestions on ways of getting around this? Short of just writing a run_me_not_mix_compile.sh script :slight_smile:

Most Liked

jordan0day

jordan0day

I’m definitely out of my depth here… I’d certainly prefer to statically link the vendor library, but all I have is the dynamic .so supplied by the vendor – no source code or static artifact to link against.

Your comment about the relative path put me in the right direction, I think. Linking with -Wl,-rpath="./path/to/priv" seems to work. Haven’t tested in a full release scenario with this library pulled into the main application, but I’m hopeful I can make it work. Thanks!

NobbZ

NobbZ

Any reason why you do not set it in the makefile?

OvermindDL1

OvermindDL1

In the library that loads the vendor library it should just load the vendor library from the directory it already exists in, or in a hard coded relative location or so, why not just do it that way? You can get the path of the yourself (the library) and use that path to load the vendor library too. :slight_smile:

Otherwise I ‘think’ LD_LIBRARY needs to be set ‘before’ the binary runs (the binary being the BEAM since it loads ‘your’ NIF).

Or just statically build the vendor library in to yours, that’s what I always prefer.

riccardomanfrin

riccardomanfrin

This worked out great for me with the same use case.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement