Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telling ld where to look for directories via an environment variable

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.

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

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.

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

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:

ld -V
GNU ld version 2.17.50.0.6-12.el5 20061020.
like image 212
Stephen Avatar asked Feb 09 '10 03:02

Stephen


People also ask

How do I change the library path in Linux?

How do I set the Library path under Linux operating systems? You need to use ldconfig config file and ldconfig command which creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld. so.

What is the difference between path and LD_LIBRARY_PATH?

PATH is for specifying directories of executable programs. LD_LIBRARY_PATH is used to specify directories of libraries. From other point of view, PATH is used primarily by the shell, while LD_LIBRARY_PATH is used by the dynamic loader (usually ld-linux.so ).

What is the environment variable $Ld_library_path and how do you use it?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.


1 Answers

You could try using the environment variable LIBRARY_PATH

From man gcc (at least version 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.

like image 69
Kjetil Joergensen Avatar answered Sep 23 '22 07:09

Kjetil Joergensen