0

I have a directory structure like this:

main/
    foo.f90
    bar.f90
    main/stuff
        floop.f90

foo.f90 contains a PROGRAM which includes functionality of bar.f90 via the USE keyword, and bar.f90 includes functionality of floop.f90 via USE

My Makefile looks like:

# define objects, mods
objects: foo.o bar.o floop.o
mods: foo.mod bar.mod floop.mod
FC = gfortran

# make
foo: $(objects)
    $(FC) -o foo $(objects)
floop.o : stuff/floop.f90
    $(FC) -c stuff/floop.f90
bar.o : bar.f90
    $(FC) -c bar.f90
foo.o : bar.o floop.o foo.f90
    $(FC) -c foo.f90

Trying to make this, though, I get an error from the bar.f90 source:

gfortran -c bar.f90

use floop                         
    1
Fatal Error: Can't open module file 'floop.mod' for reading at (1): No such file or directory

How do I make gfortran aware of the location of thus module? As I understand, mod files are created in the current working directory by default, so I thought that constructing the floop.o target correctly, by explicitly pointing to the source at stuff/floop.f90, would be sufficient.

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
  • Have you compiled the `floop` module source by the time you try to compile other things using that module? Your makefile snippet doesn't suggest correct decency order – francescalus Jul 22 '21 at 21:52
  • @francescalus it may be that the line `$(FC) -c stuff/floop.f90` is silently failing, though I don't know what that would be – pretzlstyle Jul 22 '21 at 21:54
  • If you delete all built objects, then what does `make -n` say? `objects: foo.o bar.o floop.o` means one expects `bar.f90` to be compiled before `stuff/floop.f90`. – francescalus Jul 22 '21 at 21:56
  • @francescalus ah, this may have fixed it... I thought the instructions to make each target just needed to be in order (i.e. the calls to gfortran), but didn't know the list of objects needed to be ordered as well – pretzlstyle Jul 22 '21 at 22:03
  • @francescalus it now seems that everything works up to the last instruction to compile `foo`, when I see that every variables from `bar.f90` that I try to access gives `undefined reference to `__bar_MOD_variable'` – pretzlstyle Jul 22 '21 at 22:07
  • 1
    The order of the recipes in the makefile is generally insignificant when it comes to order of actions. Order of dependencies matters more: see [this other question](https://stackoverflow.com/q/56576406/3157076). – francescalus Jul 22 '21 at 22:07
  • [Here](https://stackoverflow.com/q/66855252/3157076) is a good starting point for undefined reference errors. If that doesn't help, please [edit] more detail to your question (with [mre] ideally). – francescalus Jul 22 '21 at 22:12

0 Answers0