Gfortran LAPACK "undefined link" error

I installed LAPACK on Ubuntu by following the instructions

sudo apt-get install liblapack-dev 

this way I can find /usr/lib/libblas/libblas.a and /usr/lib/lapack/liblapack.a and then check it in randomsys1 using the randomsys1 example .

  gfortran -llapack -lblas randomsys1.f90 gfortran -llapack -L/usr/lib/lapack -lblas -L/usr/lib/libblas randomsys1.f90 

but I got the following errors ( dgesv is a LAPACK procedure):

 /tmp/ccnzuuiY.o: In function `MAIN__': randomsys1.f90:(.text+0xb): undefined reference to `init_random_seed_' randomsys1.f90:(.text+0x3c2): undefined reference to `dgesv_' collect2: ld returned 1 exit status 

Is there anything wrong with installing LAPACK? Thank you very much!

+6
source share
1 answer

See gcc / gfortran documentation:

-llibrary -l library

Look for a library named library when linking. (The second alternative, with the library as a separate argument, is for POSIX only and is not recommended.)

It matters when you write this option in a command; the linker searches and processes the libraries and object files in which they are listed. Thus, 'foo.o -lz bar.o looks for the library' z after the file foo.o, but before bar.o. If bar.o refers to functions in 'Z, these functions cannot be loaded.

The component searches for a standard list of directories for the library, in fact it is a file called liblibrary.a. The linker then uses this file as if it were specified exactly by name.

Distorted directories include several standard system directories, plus everything you specify with -L.

Typically, files found this way are library files, archive files of which the object files are members. The compiler processes the archive file and scans through it for members that identify characters that have so far been specified but not defined. But if the found file is a regular object file, it is linked in the usual way. The only difference between using the -l option and specifying the file name is that -l surrounds the library with 'lib and'.a and looks for multiple directories.

So, you must first set -L/directory/of/the/library so that the compiler knows the directory containing your library, and then the -llibrary flag.

+5
source

All Articles