问题
I have installed LAPACK and BLAS from Synaptic package manager in Ubuntu.
whereis libblas
libblas: /usr/lib/libblas.so /usr/lib/libblas.a /usr/lib/libblas
whereis liblapack
liblapack: /usr/lib/liblapack.a /usr/lib/liblapack.so
When I try to compile the randomsys1 example with gfortran
I get the following error messages.
gfortran randomsys1.f90 -L/usr/lib/lapack -llapack -L/usr/lib/libblas -lblas
/tmp/cclwtifh.o: In function `MAIN__':
randomsys1.f90:(.text+0x12): undefined reference to `init_random_seed_'
collect2: error: ld returned 1 exit status
or
gfortran randomsys1.f90 -llapack -lblas
/tmp/ccB1isEC.o: In function `MAIN__':
randomsys1.f90:(.text+0x12): undefined reference to `init_random_seed_'
collect2: error: ld returned 1 exit status
As per my understanding, it is the recommended way to link gfortran
with lapack
and blas
(kindly refer to gfortran LAPACK “undefined reference” error). Thanks in advance for pointing out the correct way to compile the fortran
code using gfortran
.
回答1:
Give this a try
gfortran randomsys1.f90 -L/usr/lib -llapack -L/usr/lib -lblas
I think you went one directory too far
I wrote a program using the LAPACK eigensolver and here is how I successfully compiled it on my own computer. It was used to calculate modes of a spring-mass system.
gfortran eigen.f90 -L/usr/local/lib -lblas -L/usr/local/lib -llapack
This also works on my computer
gfortran eigen.f90 -lblas -llapack
I just tried both to verify.
PS, now that you know how to compile, I think you need the subroutine init_random_seed in your program (goes after "contains" but before "end program"). This one is from google. No idea if it is what you need, your professor should be able to steer you correctly here.
! Initialize the random number generator using current time,
! so a new sequence of random numbers is generated each
! execution time.
! Taken from http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))
CALL SYSTEM_CLOCK(COUNT=clock)
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)
print *, "Using random seed = ", seed
print *, " "
DEALLOCATE(seed)
END SUBROUTINE
回答2:
I had the same problem. I followed the recommendation above and it worked. Here's my working example batch file (it has to be executed in the cmd window):
gfortran -c for\kurs_evd.f90
gfortran -o kurs_evd.exe kurs_evd.o charint.o -L/usr/lib -llapack -L/usr/lib -lblas
kurs_evd.exe
来源:https://stackoverflow.com/questions/35007006/error-in-linking-gfortran-to-lapack-and-blas