我有兩個源文件,library.f90
和toolbox.f90
在一個單獨的目錄中。它們被編譯成目標文件,在編譯主文件時被鏈接,適當地稱爲main
。起初,我有這個Makefile:爲什麼在源文件更改時不make重新編譯這些目標文件?
TARGET = main
SRC = ${TARGET}.f95
FC = /usr/bin/gfortran
CFLAGS = -O2 -std=gnu -Wfatal-errors -I${INCLUDE}
INCLUDE = /home/ma/include
OBJ = library.o toolbox.o
OBJ_SRC = $(subst .o,.f90,${OBJ})
all: ${TARGET}
${TARGET}: ${SRC} ${OBJ}
@${FC} ${CFLAGS} -pedantic -Wall ${OBJ} ${SRC} -o ${TARGET}
${OBJ}:
@echo "Compiling $(subst .o,,[email protected])"
@${FC} ${CFLAGS} -c ${INCLUDE}/$(subst .o,.f90,[email protected]) -o [email protected]
clean:
@rm -vf ${TARGET} ${OBJ}
但這並不重新編譯library.o
(或toolbox.o
,分別)的源文件library.f90
或toolbox.f90
編輯時。我想這是因爲依賴是失蹤,所以我調整了Makefile文件看起來像這樣:
TARGET = main
SRC = ${TARGET}.f95
FC = /usr/bin/gfortran
CFLAGS = -O2 -std=gnu -Wfatal-errors -I${INCLUDE}
INCLUDE = /home/ma/include
OBJ = library.o toolbox.o
all: ${TARGET}
${TARGET}: ${SRC} ${OBJ}
@${FC} ${CFLAGS} -pedantic -Wall ${OBJ} ${SRC} -o ${TARGET}
${OBJ}: ${OBJ_SRC}
@echo "Compiling $(subst .o,,[email protected])"
@${FC} ${CFLAGS} -c ${INCLUDE}/$(subst .o,.f90,[email protected]) -o [email protected]
clean:
@rm -vf ${TARGET} ${OBJ}
然而,這引發錯誤
make: *** No rule to make target 'library.f90', needed by 'library.o'. Stop.
我不明白這一點,因爲有並不是構建${SRC}
的規則,但是可以識別那裏的依賴關係並按預期編譯源文件。