Linux LD_LIBRARY_PATH 似乎不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13292261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:44:12  来源:igfitidea点击:

LD_LIBRARY_PATH doesn't seem to work

linuxgcccentosgnu

提问by

I'm trying to compile a test file:

我正在尝试编译一个测试文件:

gcc -o test  test.c -lg2c

but I get the error:

但我收到错误:

/usr/bin/ld: cannot find -lg2c

If I use:

如果我使用:

gcc -o test  test.c -L/usr/lib/gcc/x86_64-redhat-linux/3.4.6 -lg2c

then it works fine.

然后它工作正常。

So I added the path like so:

所以我添加了这样的路径:

LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/3.4.6:$LD_LIBRARY_PATH

and when I use $LD_LIBRARY_PATHit's listed there, but:

当我使用$LD_LIBRARY_PATH它时,它会在那里列出,但是:

gcc -o test  test.c -lg2c

still doesn't work, it gives the same error, I can't figure out why.

仍然不起作用,它给出了同样的错误,我不知道为什么。

I'm using CentOS (2.6.32-279.9.1.el6.x86_64), any help would be greatly appreciated.

我正在使用 CentOS (2.6.32-279.9.1.el6.x86_64),任何帮助将不胜感激。



EDIT: compiler version:

编辑:编译器版本:

rpm -qa | grep gcc

gcc-4.4.6-4.el6.x86_64
compat-gcc-34-g77-3.4.6-19.el6.x86_64
libgcc-4.4.6-4.el6.x86_64
compat-gcc-34-3.4.6-19.el6.x86_64
gcc-gfortran-4.4.6-4.el6.x86_64
libgcc-4.4.6-4.el6.i686
gcc-c++-4.4.6-4.el6.x86_64


EDIT: I tried using LIBRARY_PATHinstead, now I get a different error:

编辑:我尝试LIBRARY_PATH改用,现在出现不同的错误:

gcc: spec failure: unrecognized spec option 'M'

I have no idea what it means.

我不知道这意味着什么。

采纳答案by dogbane

Try setting LIBRARY_PATH, instead of LD_LIBRARY_PATH.

尝试设置LIBRARY_PATH,而不是LD_LIBRARY_PATH

From the gccman page:

gcc手册页:

LIBRARY_PATH

The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can't find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).

LIBRARY_PATH

LIBRARY_PATH 的值是以冒号分隔的目录列表,很像 PATH。当配置为本地编译器时,GCC 在搜索特殊链接器文件时会尝试这样指定的目录,如果它无法使用 GCC_EXEC_PREFIX 找到它们。在为 -l 选项搜索普通库时,使用 GCC 进行链接也会使用这些目录(但首先使用 -L 指定的目录)。

回答by mazayus

Make sure that you export LD_LIBRARY_PATHafter modifying it. Otherwise GCC won't be able to see the modified version.

确保LD_LIBRARY_PATH在修改后导出。否则 GCC 将无法看到修改后的版本。

LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/3.4.6:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
gcc -o test test.c -lg2c

回答by cho_uc

Create a new folder inside your home directory (not root directory!). Copy all the required library associated with -lg2cfrom /usr/lib/gcc/x86_64-redhat-linux/3.4.6to this folder.

在您的主目录(不是根目录!)中创建一个新文件夹。将与-lg2cfrom关联的所有必需库复制/usr/lib/gcc/x86_64-redhat-linux/3.4.6到此文件夹。

Set the path in your bash profile to this new folder.

将 bash 配置文件中的路径设置为这个新文件夹。

I also got this issue, and solve it with this workaround. Perhaps we (as non-root users) don't have the necessary access/right to /usr/lib/gcc/x86_64-redhat-linux/3.4.6

我也遇到了这个问题,并通过此解决方法解决了它。也许我们(作为非 root 用户)没有必要的访问/权限/usr/lib/gcc/x86_64-redhat-linux/3.4.6