Best way to specify a relative path in a relocatable way?

I’d like to specify a relative path in my code. My code is to be compiled by PackageCompiler and be used by many others, so it must be relocatable. There are mainly two points:

  1. it should behave the same wherever it is runned. That is to say, the path should be relative to where the binary exists, instead of where the binary is executed.
  2. it should run in different machines, as long as I copy all files needed.

It seems that @path macro from RelocatableFolders.jl suffices, but are there better ways? Like utilizing RPATH or RUNPATH variable?
But after simple test, I found ENV["DT_RPATH"] will not be the real RPATH for compiled binaries.

MWE, a simple package:

module SelfTest

function test_rpath()
    println(ENV["DT_RPATH"])
end

function julia_main()::Cint
    test_rpath()
    return 0
end

end

Compile it and run:

$ ./bin/bin/SelfTest 
hwloc/linux: Ignoring PCI device with non-16bit domain.
Pass --enable-32bits-pci-domain to configure to support such devices
(warning: it would break the library ABI, don't enable unless really needed).
/public/opt/tools/julia/julia-1.9.0/lib/julia:/public/opt/tools/julia/julia-1.9.0/lib
$ chrpath -l ./bin/bin/SelfTest 
./bin/bin/SelfTest: RPATH=$ORIGIN/../lib:$ORIGIN/../lib/julia
1 Like

I know that LD_LIBRARY_PATH is an environment variable, but I thought that DT_RPATH was the name of a dynamic header in an ELF file, and not an environment variable used by Linux? Thus setting ENV["DT_RPATH"] won’t do anything normally - perhaps PackageCompiler uses it.

I think that RPATH was deprecated and we were supposed to be using RUNPATH?

Yes, at least for new systems (old ones may not support RUNPATH). But I did not find a way to use that, too.
My final solution is to offer an environmental variable for users to set, because in my case, the path is only used to find some configuration files.