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

**URL:** <https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314>\
**Category:** Help\
**Created:** [September 14, 2022, 3:00pm UTC](https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314 "2022-09-14T15:00:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [September 14, 2022, 3:00pm UTC](https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314/1 "2022-09-14T15:00:49Z")

</div>

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

```auto
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:

```auto
gfortran -O3 c_binding_test.f90 -o c_binding_test

```

I get this error:

```auto
/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 :

> <https://stackoverflow.com/questions/7347959/no-output-from-a-fortran-program-using-the-gnu-scientific-library-via-a-c-wrappe/7351916#7351916>

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [September 14, 2022, 3:06pm UTC](https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314/2 "2022-09-14T15:06:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [September 14, 2022, 3:23pm UTC](https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314/3 "2022-09-14T15:23:46Z")

</div>

I used

```auto
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)

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [September 14, 2022, 3:41pm UTC](https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314/4 "2022-09-14T15:41:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [September 14, 2022, 4:15pm UTC](https://fortran-lang.discourse.group/t/trying-to-call-gsl-function-from-fortran-program-got-errors-when-trying-to-compile/4314/5 "2022-09-14T16:15:47Z")

</div>

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](https://coral.ise.lehigh.edu/jild13/2016/07/11/hello/)  
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

```auto
export LD_LIBRARY_PATH=/home/yourname/gsl/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

```

1. Run your binary and you should be ok ! 🙂
