Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ubuntu - gfortran LAPACK "undefined reference" error

I installed LAPACK on Ubuntu by following the instruction,

sudo apt-get install liblapack-dev

thus I can find /usr/lib/libblas/libblas.a and /usr/lib/lapack/liblapack.a, and then tested it in gfortran with the randomsys1 example,

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

but I received the following errors (dgesv is a LAPACK routine):

/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 to install LAPACK? Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the gcc/gfortran documentation:

-llibrary, -l library

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

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.

The directories searched include several standard system directories plus any that you specify with -L.

Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with ‘lib’ and ‘.a’ and searches several directories.

So you have to put first the -L/directory/of/the/library so the compiler is aware of the directory containing your library, and then the -llibrary flag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...