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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement