1

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:

/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.

Another User
  • 125
  • 3
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it in Fortran?](https://stackoverflow.com/questions/66855252/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – veryreverie Jun 24 '22 at 13:42

1 Answers1

2

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)
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • The command was gfortran -Wall -Wextra -llapack -lblas -o main mod_print.o mod_genmat.o mod_exp.o main.o I modified as you said, and now it's working, thanks. I'll accept the answer as soon as I'm able. – Another User Jun 24 '22 at 13:11