Libraries
Loading additional libraries is achieved by using modules.
Netlib-lapack
As an example:
$ module load netlib-lapack
Loading netlib-lapack/3.12.1-gcc-11.5.0
$ pkg-config --libs --cflags blas lapack
-L/cvmfs/software.pi.infn.it/[...]/netlib-lapack-[...]/lib64 -lblas -llapack
Use the pieces of information provided by pkg-config in gfortran/gcc invocation.:
LIBFLAGS=$(pkg-config --libs --cflags blas lapack)
LIBDIR=$(pkg-config --libs-only-L lapack blas | sed 's/-L//g')
gfortran [...] -Wl,-rpath,"$LIBDIR" $LIBFLAGS [...]
Building with -Wl,rpath,.. will produce binaries with hardcoded runpath: no need to modify LD_LIBRARY_PATH to launch them! The order of the arguments in gfortran is important!
GSL
Another example, to build fortran code with gsl:
module load fgsl
LIBFLAGS=$(pkg-config --libs --cflags gsl)
LIBDIR=$(pkg-config --libs-only-L gsl | sed 's/-L//g')
gfortran -w -fPIC -m64 -O3 -fno-second-underscore -mcmodel=large -Wl,-rpath,$LIBDIR $LIBFLAGS [...]