告诉ld通过环境变量查找目录的位置

时间:2022-09-18 11:43:37

I'm grading C and C++ files for a class, and this assignment uses the GSL library. Since I don't have root permission on my computer, my GSL library is installed in my home directory, and thus I need to tell compilers and linkers where to find it.

我正在为一个类评分C和C ++文件,这个赋值使用GSL库。由于我的计算机上没有root权限,因此我的GSL库安装在我的主目录中,因此我需要告诉编译器和链接器在哪里找到它。

This isn't a problem when I write a program myself, because I just add the appropriate -L and -I flags to gcc.

当我自己编写程序时,这不是问题,因为我只是在gcc中添加了适当的-L和-I标志。

But when I'm compiling student's files, I don't want to edit every one of their makefiles. Instead, I want to put the appropriate directories into an environment variable, so that it happens seamlessly.

但是当我编译学生的文件时,我不想编辑他们的每个makefile。相反,我想将适当的目录放入环境变量中,以便无缝地发生。

To this end, I've exported the following variables with the library or include locations: C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, LIBRARY_PATH and LD_LIBRARY_PATH

为此,我已经使用库或包含位置导出了以下变量:C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,LIBRARY_PATH和LD_LIBRARY_PATH

But when I compile a student's project, with

但是当我编译学生的项目时,用

gcc -Wall -o MC_thread MC_thread.c -lgsl -lgslcblas -lpthread -lm

I get the following error:

我收到以下错误:

/usr/bin/ld: cannot find -lgsl
collect2: ld returned 1 exit status
make: *** [all] Error 1

I'm using gcc v 4.1.2. I actually don't get the error if I use gcc v 4.4, but I have no clue why. My linker is:

我正在使用gcc v 4.1.2。如果我使用gcc v 4.4,我实际上没有得到错误,但我不知道为什么。我的链接器是:

ld -V
GNU ld version 2.17.50.0.6-12.el5 20061020.

4 个解决方案

#1


11  

You could try using the environment variable LIBRARY_PATH

您可以尝试使用环境变量LIBRARY_PATH

From man gcc (at least version 4.4)

来自man gcc(至少版本4.4)

       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).

And then then use LD_LIBRARY_PATH when you run their programs to to let the run-time linker find the libraries.

然后在运行程序时使用LD_LIBRARY_PATH,让运行时链接程序找到库。

#2


3  

A lot of the answers above suggest the use of LD_LIBRARY_PATH. But this is incorrect since that is an environmental variable for the dynamic (runtime) linker, not the compile time linker ld.

上面的很多答案都建议使用LD_LIBRARY_PATH。但这是不正确的,因为这是动态(运行时)链接器的环境变量,而不是编译时链接器ld。

The correct way to do this is to require the students to append something like:

正确的方法是要求学生追加以下内容:

-L$(EXTRA_LINK_DIRECTORY)

in their Makefile at the point at which they define the build rule. Then, when you compile, do something like:

在他们的Makefile中定义构建规则的位置。然后,在编译时,执行以下操作:

export EXTRA_LINK_DIRECORY=/home/...

#3


1  

If you're on a 64-bit machine, that's probably the problem. OMM, gcc 4.1 doesn't search the paths specified in LIBRARY_PATH, but rather path/../lib64. You'll need to specify -L directly, or symlink the directory to lib64 at the same level, or mess with the gcc specs.

如果您使用的是64位计算机,那可能就是问题所在。 OMM,gcc 4.1不搜索LIBRARY_PATH中指定的路径,而是搜索路径/../ lib64。您需要直接指定-L,或者将目录符号链接到同一级别的lib64,或者使用gcc规范。

See http://gcc.gnu.org/ml/gcc-help/2010-11/msg00360.html and Why does g++ look in LIBRARY_PATH/../lib64 and where is this documented?

请参阅http://gcc.gnu.org/ml/gcc-help/2010-11/msg00360.html以及为什么g ++看起来在LIBRARY_PATH /../ lib64中,这在哪里有记录?

(OMM, this does work with gcc 4.5 without any messing around, so I'm guessing they fixed it later on.)

(OMM,这确实适用于gcc 4.5而没有任何混乱,所以我猜他们以后修复它。)

#4


0  

My advice is to require students to support a CFLAGS environment variable in their makefiles, Or else they fail. :) Then you can export CFLAGS="-Lwhatever".

我的建议是要求学生在其makefile中支持CFLAGS环境变量,否则它们会失败。 :)然后你可以导出CFLAGS =“ - Lwhatever”。

Or you could use LD_LIBRARY_PATH.

或者您可以使用LD_LIBRARY_PATH。

#1


11  

You could try using the environment variable LIBRARY_PATH

您可以尝试使用环境变量LIBRARY_PATH

From man gcc (at least version 4.4)

来自man gcc(至少版本4.4)

       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).

And then then use LD_LIBRARY_PATH when you run their programs to to let the run-time linker find the libraries.

然后在运行程序时使用LD_LIBRARY_PATH,让运行时链接程序找到库。

#2


3  

A lot of the answers above suggest the use of LD_LIBRARY_PATH. But this is incorrect since that is an environmental variable for the dynamic (runtime) linker, not the compile time linker ld.

上面的很多答案都建议使用LD_LIBRARY_PATH。但这是不正确的,因为这是动态(运行时)链接器的环境变量,而不是编译时链接器ld。

The correct way to do this is to require the students to append something like:

正确的方法是要求学生追加以下内容:

-L$(EXTRA_LINK_DIRECTORY)

in their Makefile at the point at which they define the build rule. Then, when you compile, do something like:

在他们的Makefile中定义构建规则的位置。然后,在编译时,执行以下操作:

export EXTRA_LINK_DIRECORY=/home/...

#3


1  

If you're on a 64-bit machine, that's probably the problem. OMM, gcc 4.1 doesn't search the paths specified in LIBRARY_PATH, but rather path/../lib64. You'll need to specify -L directly, or symlink the directory to lib64 at the same level, or mess with the gcc specs.

如果您使用的是64位计算机,那可能就是问题所在。 OMM,gcc 4.1不搜索LIBRARY_PATH中指定的路径,而是搜索路径/../ lib64。您需要直接指定-L,或者将目录符号链接到同一级别的lib64,或者使用gcc规范。

See http://gcc.gnu.org/ml/gcc-help/2010-11/msg00360.html and Why does g++ look in LIBRARY_PATH/../lib64 and where is this documented?

请参阅http://gcc.gnu.org/ml/gcc-help/2010-11/msg00360.html以及为什么g ++看起来在LIBRARY_PATH /../ lib64中,这在哪里有记录?

(OMM, this does work with gcc 4.5 without any messing around, so I'm guessing they fixed it later on.)

(OMM,这确实适用于gcc 4.5而没有任何混乱,所以我猜他们以后修复它。)

#4


0  

My advice is to require students to support a CFLAGS environment variable in their makefiles, Or else they fail. :) Then you can export CFLAGS="-Lwhatever".

我的建议是要求学生在其makefile中支持CFLAGS环境变量,否则它们会失败。 :)然后你可以导出CFLAGS =“ - Lwhatever”。

Or you could use LD_LIBRARY_PATH.

或者您可以使用LD_LIBRARY_PATH。