Search code examples
c++linker-errorsstatic-linkinggsl

Linking to GNU scientific library on cluster?


I am running a code (iHARM2D) which requires the GNU scientific library library (GSL) on a cluster. Since the GSL library is not installed on the cluster, I have to compile it there and properly link it during compilation of the actual code. In my shell script I write

cd whereGSLsource
./configure --prefix=/homefolder/iHARM/GSLcompiled
make && make install

This compiles the GSL and puts the results in /homefolder/iHARM/GSLcompiled/lib, /homefolder/iHARM/GSLcompiled/include etc.

According to this answer, I should be able to compile by writing the following lines into my shell script before compilation of my main code

export CPATH="/homefolder/iHARM/GSLcompiled/include":$CPATH
export LIBRARY_PATH="/homefolder/iHARM/GSLcompiled/lib":$LIBRARY_PATH

However, this does not seem to link GSL properly because the compilation returns errors of the type "undefined reference to `gsl_some_function'". (It works on my computer when default installation and linking of GSL is used.)

Another possibility suggested by the GSL output during compilation or this answer is to modify the LD_LIBRARY_PATH variable

LD_LIBRARY_PATH="/homefolder/iHARM/GSLcompiled/lib":$LD_LIBRARY_PATH

But this gives the same result. Similarly it does not when I try to link using the -L and -I option

cd iHARM
gcc -someoptions -I../GSLcompiled/include/ -L../GSLcompiled/lib ./some.o -o harm

Another option suggested by GSL was to use

gcc -someoptions -Wl,-rpath -Wl,"/homefolder/iHARM/GSLcompiled/lib" ./some.o -o harm

However, neither of these work.

How do I link the GSL properly then?

(I am not very experienced in this so this might also be some really basic mistake in the syntax or so.)


Solution

  • Run first configure --help; you'll find out that it accepts the --enable-static option which you do want to use.

    BTW you could (and probably should) install Linux on your laptop and compile on it (then scp a mostly statically linked binary to your cluster).

    You'll better share a common --prefix for all your autoconf-ed software. See this. Read the documentation of autoconf. Let's suppose you always use --prefix=$HOME/soft (which don't require any root permission).

    You could compile with make then do a make install DESTDIR=/tmp/gslinst so that installed things go into /tmp/gslinst which you would inspect and latter copy appropriately to a directory related to your prefix.

    You'll find both libgsl.a and libgslcblas.a. On my Debian system, the libgsl-dev package provides them (so I don't need to rebuild it).

    Then you'll use these static libraries. You could provide a full path for them, that is use $HOME/soft/lib/libgsl.a explicitly in your linking gcc command for harm, e.g. link it with

     gcc some.o $HOME/soft/lib/libgsl.a -o harm
    

    but YMMV. Order of arguments to gcc matters a lot.

    You don't need or want to mess with $LD_LIBRARY_PATH or -Wl,-rpath with static linking. Read about rpath when you want dynamic linking.

    See also what pkg-config tells.