PeMa
PeMa

Reputation: 1716

Make recognizes only changed object

I'm a bit confused. I have a makefile to creat a target depending on some objects. If I change one of the objects and run the makefile again, make only links the changed object. This of course leads to an error. I'm sure this error occures, since my makefile isn't clear enough. So could someone please tell me whats wrong? I guess one should tell make anyhow that only the specified objects are to be compiled, but than I would need something like a loop to create the Objects. ... I don't know.

The Makefile:

F90=gfortran

SRCF=./src
OBJF=./objs
MODF=./mods
BINF=./bin

SOURCES=dep.f90 main.f90
OBJECTS=$(addprefix $(OBJF)/,$(addsuffix .o,$(basename $(SOURCES))))
MODULES=$(addprefix $(MODF)/,*.mod)
TARGET=main

$(BINF)/$(TARGET): $(OBJECTS)
    $(F90) -o$@ $? -J$(MODF) -I$(MODF)

clean :
    rm $(OBJECTS) $(MODULES) $(BINF)/$(TARGET)

$(OBJF)/%.o: $(SRCF)/%.f90
    $(F90) $(F90FLAGS) $(LDFLAGS) -c $< -o $@ -J$(MODF) -I$(MODF)

The error after changing dep.f90:

gfortran   -c src/dep.f90 -o objs/dep.o -J./mods -I./mods
gfortran -obin/main objs/dep.o -J./mods -I./mods 
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.5.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [bin/main] Error 1

Two sample files: main.f90:

program test

use dep
implicit none
real::a,b,c

a=1e0
b=2e0

c=summe(a,b)

print*,c

end program

dep.f90

module dep
implicit none

contains

function summe(a,b) result(c)
real::a,b,c

c=a+b

end function summe
end module dep

Upvotes: 0

Views: 63

Answers (1)

MadScientist
MadScientist

Reputation: 101081

That's what the $? variable means: it expands to only the changed prerequisites. Write your rule to use $^, which expands to all prerequisites, instead:

$(BINF)/$(TARGET): $(OBJECTS)
        $(F90) -o$@ $^ -J$(MODF) -I$(MODF)

See Automatic Variables for details.

Upvotes: 2

Related Questions