pdupre has asked for the wisdom of the Perl Monks concerning the following question:

I need to modify the variable $LD_LIBRARY_PATH for some of my perl applications

Following some recommendations of the list, I tried in my perl application:

BEGIN { $ENV{LD_LIBRARY_PATH} = "/usr/local/gsl-1/lib" ; # exec 'env', $^X, $0, @ARGV ; }

Actually, exec 'env', $^X, $0, @ARGV ; does not work

However, if I run

LD_LIBRARY_PATH=/usr/local/gsl-1/lib ./myperl.pl

It woks fine

How can I modify the LD_LIBRARY_PATH inside the perl file?

Patrick Dupre Universite du Littoral, FR Dunkirk

Replies are listed 'Best First'.
Re: LD_LIBRARY_PATH setting
by Corion (Patriarch) on Aug 05, 2016 at 18:31 UTC

    On most operating systems, $^X contains the full path to the perl executable used to run your script, so you should not need to involve env.

    Your approach is the correct approach to restart your Perl script with added parameters, but you need to add one more parameter that indicates that your Perl script has restarted itself already:

    BEGIN { if( ! $ENV{PDUPRE_RESTART}) { $ENV{LD_LIBRARY_PATH} = '...'; $ENV{PDUPRE_RESTART} = 1; exec $0, '--restarted', @ARGV }; }; ...

      Thank

      But it still does not work!

      it just freezes

      and finally (after a timeout?) ignores what is in the enclosure

      Patrick Dupre Universite du Littoral, FR Dunkirk

        Maybe you can print some output so you see what is happening?

        Alternatively, trace your program with truss or strace to see what system calls it makes.

        Maybe you can post a short, self-contained example of the program you're actually running so we can reproduce your problem exactly?

Re: LD_LIBRARY_PATH setting
by Anonymous Monk on Aug 05, 2016 at 19:15 UTC
    LD_LIBRARY_PATH is for when you want to run a program and change which .so gets loaded, its like $PATH or %PATH%,

    Its not for compiling stuff like LIBS argument to Makefile.PL

      Oh, sorry , I got confused, I am wrong, you're trying the right direction

Re: LD_LIBRARY_PATH setting
by duyet (Friar) on Aug 06, 2016 at 12:11 UTC
    When i need a path for my script i just included in the @INC, ie.
    BEGIN { # ... unshift @INC, '/usr/local/gsl-1/lib'; # .. }

      $ENV{LD_LIBRARY_PATH} is where unixish/linuxish systems go looking for system libraries. This comes into play when Perl libraries need to load system libraries. @INC is where Perl goes looking for Perl modules/Perl libraries.

        Worth adding that it should be possible to compile the related perl .so modules with a path to their dependencies, if necessary. The CC option -Wl,--rpath=/path/to/libs is passed to linker; resulting shared objects ought to load without custom LD_LIBRARY_PATH. So it's a matter of properly done module install.