set library path for current script

35,008

Solution 1

In your script, these two lines close to the top should do the trick:

LD_LIBRARY_PATH="$(pwd)/lib"
export LD_LIBRARY_PATH

Although bash allows you to set and export a variable in a single statement, not all shells do, so the two step approach is more portable, if that's a concern.

If this isn't working for you, check that you are running the script from the right place - using $(pwd) like this ties you to running the script from the directory that contains the required ./lib subdirectory.

If you want to be able to run the script from anywhere, you need to use the absolute path to the ./lib subdir, or construct a relative path from the directory portion of the path to the script using, e.g., $(dirname $0)

Solution 2

you should execute you program in this way:

LD_LIBRARY_PATH=$(pwd)/lib/ <your_executable_here>
Share:
35,008

Related videos on Youtube

nifker
Author by

nifker

Updated on September 18, 2022

Comments

  • nifker
    nifker almost 2 years

    How can I set the library path for the current script that's running? I mean I don't want to list a new path for the libraries in a textfile. I tried it using export LD_LIBRARY_PATH=$(pwd)/lib/

    This is the script:

    #!/bin/bash
    LD_LIBRARY_PATH="$(pwd)/lib/"
    export LD_LIBRARY_PATH
    ./X3TC_config
    
    • Jazz
      Jazz about 9 years
      Do you really mean $(pwd), or do you mean $(dirname $0)?
    • nifker
      nifker about 9 years
      I mean $(pwd) .
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' about 9 years
      The script you've posted does exactly what you're asking. If that's not working for you, tell us where the libraries are located, what the current directory is when you start the script, and copy-paste the error messages complaining of a missing library. I suspect that you're asking the wrong question but I can't tell what the right question would be with so little information.
    • nifker
      nifker about 9 years
      The libs are in the lib folder but it still says it cant find the "libgtk-x11-2.0.so.0" library
  • Atul Vekariya
    Atul Vekariya about 9 years
    @D_Bye, probably. But if you need this setting only for this script your way will mess other programs
  • Anthon
    Anthon about 9 years
    This doesn't change anything for the script that is running (which is what the OP asks for), only for a new invocation.
  • nifker
    nifker about 9 years
    Do you mean with <your_execition_here> a library?
  • Atul Vekariya
    Atul Vekariya about 9 years
    No, by <your_executable_here> i mean the program you run. If its for example script, named testscript.sh the command will become: LD_LIBRARY_PATH=$(pwd)/lib/ testscript.sh
  • nifker
    nifker about 9 years
    I use the following script: "#!/bin/bash export LD_LIBRARY_PATH=$(pwd)/lib/ ./X3TC_config" To run a game outside from steam.
  • nifker
    nifker about 9 years
    Isn't it possible to set library path in the script?
  • Jazz
    Jazz about 9 years
    It is possible to export a variable in a script, so that it only affects calls made within that script. Once the shell started by the kernel to run the script dies, so does its context - including any variables you export. I should have made that clearer in my comment.
  • Atul Vekariya
    Atul Vekariya about 9 years
    Yes, you can do it by executing your script on this way: . /path/to/script.sh This will run the script in current shell
  • nifker
    nifker about 9 years
    I tried to run it like that but it says it can't find the libraries that are in the lib folder
  • Atul Vekariya
    Atul Vekariya about 9 years
    This way is only if you have the set of variable inside the script
  • nifker
    nifker about 9 years
    It still says it can't the required "libgtk-x11-2.0.so.0" although it is in the folder.
  • Jazz
    Jazz about 9 years
    What is the command you are trying to call? Does it care about the value of LD_LIBRARY_PATH?
  • nifker
    nifker about 9 years
    chmod +x testandlaunch ./testandlaunch
  • Jazz
    Jazz about 9 years
    Is testandlaunch the program you are trying to set LD_LIBRARY_PATH for, or the script you are trying to modify to make that happen? It might help if you post the relevant parts of your script as an edit to your question.
  • nifker
    nifker about 9 years
    It's the script.