3

The following example makefile works as expected, using vpath to find object files and source files. But in the last line, where i tell make about the dependency of one object file on the other, I need to specify the directory $(objd)/ of the prerequisite file, otherwise i get an error (see error message below the code). How come the vpath directive isn't sufficient in the last line?

# Program Name
prog = avpar

#dirs
objd=obj
modd=mod


# extra places to search for prerequisites
vpath %.f90 ../modules
vpath %.o obj/

# etc
FC      = gfortran
flags       = -I$(modd) -J$(modd) #-fopenmp

obj_files   = $(prog).o rw_mod.o 

# compile
p$(prog): $(obj_files)    
    $(FC)  $(flags) $^ -o $@

$(objd)/%.o: %.f90  
    $(FC)  $(flags) -c $< -o $@

$(objd)/$(prog).o: $(objd)/rw_mod.o

That is, changing the last line to:

$(objd)/$(prog).o: rw_mod.o

gives the error:

make: *** No rule to make target 'rw_mod.o', needed by 'obj/avpar.o'.  Stop.

EDIT with this form of the last lines it does also work, without the directory specification:

#compile
p$(prog): $(obj_files)    
    $(FC)  $(flags) $^ -o $@

$(objd)/rw_mod.o: rw_mod.f90 
    $(FC)  $(flags) -c $< -o $@

$(objd)/$(prog).o: $(prog).f90 rw_mod.o
    $(FC)  $(flags) -c $< -o $@
Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27

1 Answers1

1

vpath can only be used to find prerequisites that exist.

Makefiles rule 3

Use VPATH to locate the sources from the objects directory, not to locate the objects from the sources directory.

There's no rule that matches rw_mod.o so the rule for obj/avpar.o fails, vpath won't prepend stuff during prerequisite rule lookup, the only way it would work here would be if obj/rw_mod.o already existed.

It's unlikely that rule is correct anyway, why would one object file depend on another?

user657267
  • 20,568
  • 5
  • 58
  • 77
  • I added a clarification. You might be right that it is because the file does not exist. On the other hand, none of the files exist in the first line of the compile statement when it is first checked. And the end rule is definitely valid, I think it is a quite standard way to state the dependency, without considering the 'vpath' question. – Jonatan Öström Jul 15 '16 at 23:15
  • Well, actually, you migth be rigth that it's odd to say that the object file depend on another. The fact of the matter is that compiling to avpar.o requires the file rw_mod.mod that exist only when rw_mod.o exists, but the module files are handled separately with the compiler flags, so that is why one states that there is a dependency on the object file instead. – Jonatan Öström Jul 15 '16 at 23:20