Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't use LAPACK in makefile

I have program (in fortran) where I’m using three custom modules, which make use of LAPACK. Until now I’ve compiled my program using the following shell script:

  filestring="main"
  gfortran -c mod_exp.f90 mod_genmat.f90 mod_print.f90 $filestring.f90
  gfortran mod_exp.o mod_genmat.o mod_print.o $filestring.o -llapack -lblas
  rm mod_exp.o mod_genmat.o mod_print.o $filestring.o exponentiate.mod genmat.mod printing.mod printing_subrtns.mod
  mv a.out $filestring

Since I’ve been using more and more modules and different programs using them, I’ve decided to start using makefiles. Following a tutorial, I managed to write the following:

FC = gfortran
FFLAGS = -Wall -Wextra -llapack -lblas #-fopenmp
SOURCES = mod_print.f90 mod_genmat.f90 mod_exp.f90 main.f90
OBJ = ${SOURCES:.f90=.o} #substitute .f90 with .o


%.o : %.f90 #creation of all *.o files DEPENDS on *.f90
  $(FC) $(FFLAGS) -c -O $< -o $@
  
main: $(OBJ)
  $(FC) $(FFLAGS) -o $@ $(OBJ)

clean:
  @rm -f *.o *.mod main

However, when executing make, it says that the LAPACK functions are not recognized. One such mistake is the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/usr/bin/ld: mod_exp.o: in function `__exponentiate_MOD_diagun':
mod_exp.f90:(.text+0x37f): undefined reference to `zgees_'
...
collect2: error: ld returned 1 exit status

One possible mistake I’ve seen is that I need to specify the location of the libraries. However, it would seem strange since I didn’t need to do it before; also, I don’t know how to find it.

>Solution :

Please show the link command that make invoked, that caused the error to be generated.

I’m confident that if you cut and paste that exact command line to your shell prompt, you will get the same error you see when make runs it. So the problem is not make, but your link command.

The problem is that you have put the libraries before the objects in the link line. Libraries should come at the end, after the objects, else when the linker examines the libraries it doesn’t know what symbols will need to be included (because no objects have been parsed yet to see what symbols are missing).

This is why LDLIBS is traditionally a separate variable:

FC = gfortran
FFLAGS = -Wall -Wextra #-fopenmp
LDLIBS = -llapack -lblas
SOURCES = mod_print.f90 mod_genmat.f90 mod_exp.f90 main.f90
OBJ = ${SOURCES:.f90=.o} #substitute .f90 with .o

%.o : %.f90 #creation of all *.o files DEPENDS on *.f90
        $(FC) $(FFLAGS) -c -O $< -o $@

main: $(OBJ)
        $(FC) $(FFLAGS) -o $@ $(OBJ) $(LDLIBS)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading