Trying to call GSL function from Fortran program, got errors when trying to compile

Hi
I just discovered the GNU scientific library, which has a lot of pre written code that one can use.
I tried out an example program, just to see and learn how to properly call the gsl functions using the ISO_C_BINDING, but I got errors when trying to compile

PROGRAM c_binding_test                                                      
  USE ISO_C_BINDING                                                         
  IMPLICIT NONE                                                             
                                                                            
  INTERFACE GSL_CumulativeChiSq_Prob_Upper                                  
     FUNCTION gsl_cdf_chisq_Q (x, nu) BIND (C, name="gsl_cdf_chisq_Q")      
       IMPORT                                                               
                                                                            
       REAL (KIND=c_double) :: gsl_cdf_chisq_Q                              
                                                                            
       REAL (KIND=c_double), VALUE, INTENT(IN) :: x                         
       REAL (KIND=c_double), VALUE,INTENT(IN) :: nu                         
                                                                            
     END FUNCTION gsl_cdf_chisq_Q                                           
                                                                            
  END INTERFACE GSL_CumulativeChiSq_Prob_Upper                              
                                                                            
  REAL (KIND=c_double) :: chisq_value                                       
  REAL (KIND=c_double) :: DoF                                               
  REAL (KIND=c_double) :: Upper_Tail_Prob                                   
                                                                            
  PRINT *, "Calculates cumulative upper-tail probability for Chi-Square dis"
  PRINT *, "Input Chisq value, Degrees of Freedom"                          
  READ *, chisq_value,DoF                                                   
                                                                            
  Upper_Tail_Prob = gsl_cdf_chisq_Q (chisq_value,DoF)                       
                                                                            
  PRINT *, "Probability is: ",Upper_Tail_Prob                               
                                                                            
END PROGRAM c_binding_test                                                  

Command used to compile:

gfortran -O3 c_binding_test.f90 -o c_binding_test

I get this error:

/usr/bin/ld: /tmp/ccnF6X0E.o: in function `MAIN__':
c_binding_test.f90:(.text+0xf1): undefined reference to `gsl_cdf_chisq_Q'
collect2: error: ld returned 1 exit status

How can I correct this error ?

I got the code from here :

You need to also tell the compiler to link to the library containing that function. I.e. include a flag like -lgnusci in your compile command, possibly also with -Lpath/to/lib to tell it where to find the library.

2 Likes

I used

gfortran -O3 -L/usr/lib64 c_binding_test.f90 -o c_binding_test

still obtained an error
I installed gsl using the Fedora package manager. (didn’t build it on my own)

I’d bet that /usr/lib64 is already in gfortran’s default library search path, so that one is likely unnecessary. You’re still missing the lowercase -l flag though. Possibly -lgsl, but you may need to confirm that name.

1 Like

Got it to work !!
Thank you @everythingfunctional for trying.

To anyone else trying to get the GSL to work here is what you should do:

  1. Follow the steps on this page to download and install the GSL package (I recommend against installing it via the package manager)
    How to Install GSL on Linux (Ubuntu, Centros, Redhat, Mac OS) + Simple Installation of gcc Compilers
    a. wget "https://mirror.ibcp.fr/pub/gnu/gsl/gsl-latest.tar.gz"
    b. extract it tar -zxvf gsl-*.*.tar.gz
    c. Enter the directory cd gsl-*.*
    d. make a directory where you want to install the gsl mkdir /home/yourname/gsl
    e. configure the installation to use the new directory ./configure --prefix=/home/yourname/gsl
    f. Compile the library make
    h. check the library and test it make check
    i. install the lib make install
  2. When compiling your files gfortran -lgsl -lgslcblas -Lhome/yourname/gsl/lib foo.f90 -o foo
  3. Before you run the binary :
    Add this to your .bashrc
export LD_LIBRARY_PATH=/home/yourname/gsl/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

  1. Run your binary and you should be ok ! :slight_smile:
2 Likes