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
FUNCTION gsl_cdf_chisq_Q (x, nu) BIND (C, name="gsl_cdf_chisq_Q")
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.
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.
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:
- 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
b. extract it
tar -zxvf gsl-*.*.tar.gz
c. Enter the directory
d. make a directory where you want to install the gsl
e. configure the installation to use the new directory
f. Compile the library
h. check the library and test it
i. install the lib
- When compiling your files
gfortran -lgsl -lgslcblas -Lhome/yourname/gsl/lib foo.f90 -o foo
- Before you run the binary :
Add this to your .bashrc
- Run your binary and you should be ok !