设置LD_LIBRARY_PATH环境变量,以便在运行时加载共享库(g++)

时间:2022-12-30 22:42:03

I'm having two problems related to the same issue:

我遇到了两个与这个问题有关的问题:

  1. I have a shared object saved in `pwd`/lib and while the executable that uses it compiles successfully (by using -l and -L switches), at runtime, it's giving me grief. If I try to run LD_LIBRARY_PATH=/my/absolute/path/to/library/directory ./test it works fine. But if I export LD_LIBRARY_PATH=/my/absolute/path/to/library/directory and do ./test it says that it can't find the shared library. However, if I do LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test again it works fine!! Any ideas on what I'm doing wrong?

    我有一个在“pwd”/lib中保存的共享对象,而使用它的可执行文件在运行时成功编译(通过使用-l和-l开关),这让我感到悲伤。如果我尝试运行LD_LIBRARY_PATH=/my/absolute/path/to/library/directory ./测试,效果很好。但是如果我导出LD_LIBRARY_PATH=/my/absolute/path/to/library/directory,并执行/test,它会说它找不到共享库。但是,如果我做LD_LIBRARY_PATH=$LD_LIBRARY_PATH。你知道我做错了什么吗?

  2. Second issue is related to the exporting of the LD_LIBRARY_PATH env variable. If I open a terminal and type export LD_LIBRARY_PATH=/path/to/stuff and then type echo $LD_LIBRARY_PATH, the variable is correct. However if I write a script containing the export command, simply running it doesn't update the variable, instead I need to run source install.sh in order to actually persist the variable. What's the best solution for this?

    第二个问题与导出LD_LIBRARY_PATH env变量有关。如果我打开一个终端,输入export LD_LIBRARY_PATH=/path/to/stuff,然后输入echo $LD_LIBRARY_PATH,这个变量是正确的。但是,如果我编写一个包含export命令的脚本,仅仅运行它并不更新变量,而是需要运行source install。为了使变量持久存在。最好的解决办法是什么?

Thank you for your time!

谢谢您的时间!

2 个解决方案

#1


5  

To answer the second question first:

首先回答第二个问题:

source executes the script inside the current shell, ./install.sh opens and executes it in a different shell. http://www.unix.com/unix-dummies-questions-answers/537-difference-between-source-exec-script.html

源代码在当前shell ./install中执行脚本。sh在另一个shell中打开并执行它。http://www.unix.com/unix-dummies-questions-answers/537-difference-between-source-exec-script.html

Now for your first question:

现在来回答你的第一个问题:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test sets the LD_LIBRARY_PATH variable before just one command (the ./test command). For the same reason above, I believe this isn't getting transferred to whatever shell ./test creates. To make it persist, you may need to put the export LD_LIBRARY_PATH=... in your ~/.bashrc

LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test将LD_LIBRARY_PATH变量设置为一个命令(./test命令)。出于同样的原因,我认为它不会转移到shell中。要使其持久,您可能需要将导出LD_LIBRARY_PATH=…在你的~ / . bashrc

#2


1  

I have found sometimes adding -L explicitly via the CFLAGS environment variable is successful when LD_RUN_PATH was not. As in: export CFLAGS=-L/opt/tool/lib

我发现有时通过CFLAGS环境变量显式地添加-L是成功的,而LD_RUN_PATH不是。如:出口CFLAGS = - l / opt /工具/ lib

#1


5  

To answer the second question first:

首先回答第二个问题:

source executes the script inside the current shell, ./install.sh opens and executes it in a different shell. http://www.unix.com/unix-dummies-questions-answers/537-difference-between-source-exec-script.html

源代码在当前shell ./install中执行脚本。sh在另一个shell中打开并执行它。http://www.unix.com/unix-dummies-questions-answers/537-difference-between-source-exec-script.html

Now for your first question:

现在来回答你的第一个问题:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test sets the LD_LIBRARY_PATH variable before just one command (the ./test command). For the same reason above, I believe this isn't getting transferred to whatever shell ./test creates. To make it persist, you may need to put the export LD_LIBRARY_PATH=... in your ~/.bashrc

LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test将LD_LIBRARY_PATH变量设置为一个命令(./test命令)。出于同样的原因,我认为它不会转移到shell中。要使其持久,您可能需要将导出LD_LIBRARY_PATH=…在你的~ / . bashrc

#2


1  

I have found sometimes adding -L explicitly via the CFLAGS environment variable is successful when LD_RUN_PATH was not. As in: export CFLAGS=-L/opt/tool/lib

我发现有时通过CFLAGS环境变量显式地添加-L是成功的,而LD_RUN_PATH不是。如:出口CFLAGS = - l / opt /工具/ lib