Saturday, July 28, 2012

How to install a C/C++ library on a Linux which you cannot use “su” or “sudo”

If you are working with a remote server, you may not have administrator privilege, or “sudo”, to install a library like “yum install XXXXX” or “configure->make->make install”. But if you really need a C/C++ library and no one you known has administrator privilege, the following steps may save you, in most cases. I will use GNU Scientific Library(GSL) as an example to show you how to get it working in your own directory without administrator privilege.

1. Download GSL source code and input “-prefix=[PATH]” when running “configure”. This operation tells the source code that you want to put the library files in [PATH]. Once the source code is compiled, run “make install”. All the library files generated will be copied to [PATH].

2. Tell the compiler and linker where is the library. For GCC/G++, we could use “-L” to input a library path and “-I” to input a include path to the compiler. So, in your make file, we could define two lines as follows:

GSL_INCLUDE = /home/hanyu/library/gsl/include/
GSL_LIB = /home/hanyu/library/gsl/lib/

Then put those two flags in your make file as follows:

$(CC)  -I$(GSL_INCLUDE) -L$(GSL_LIB)

After doing the steps above, you should successfully compile your code which depends on GSL. However, when you run your program, you may receive a error like “cannot find XXXX.so…”. To fix this, please do the following step.

3. Add the library folder to “LD_LIBRARY_PATH”. Put the following lines at the bottom to your “.bashrc” and log off:

LD_LIBRARY_PATH=/home/hanyu/library/gsl/lib/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

Then, log in again. Your program should be executed properly.

No comments:

Post a Comment