Makefile编译了所有目标文件,而不是最新的修改文件

问题描述

我已经检查过this answer,但我认为情况与我不同。

我正在使用f2py编译我的fortran代码,以便可以在python中导入此fortran代码。
例如,我有两个fortran文件:t.f90t2.f90,并使用此命令进行编译:

$ f2py -c t.f90 -m f90t
$ f2py -c t2.f90 -m f90t2

并生成两个.so文件:f90t.cpython-36m-x86_64-linux-gnu.sof90t2.cpython-36m-x86_64-linux-gnu.so

我想使用Makefile来编译这些文件。我的Makefile是这样的:

FILES = t.f90 t2.f90

NAMES = $(basename $(FILES))
TARGET = $(addprefix f90,$(NAMES))

all : $(TARGET)

f90% : %.f90
    @echo "Compile $< to $@"
    f2py -c $< -m $@

.PHONY : clean
clean : 
    rm f90*.so

Makefile可以正常运行,但是有些奇怪。如果我更改一个fortran文件的内容并执行Makefile,它仍会“编译”两个fortran文件,但实际上输出文件不会更改。

赞:

$ ls       # run.py will execute the function of t.f90 and t2.f90
Makefile run.py t.f90 t2.f90
$ make
Compile t.f90 to f90t
....
Compile t2.f90 to f90t2
....
$ python run.py
"This line is printed by t.f90"
"This line is printed by t2.f90"
$ vim t2.f90     # adding some exclamation mark
$ make           # I only modified t2.f90,but t.f90 is also compiled
Compile t.f90 to f90t
....
Compile t2.f90 to f90t2
....
$ python run.py       # still the old result
"This line is printed by t.f90"
"This line is printed by t2.f90"
$ make clean
$ make
Compile t.f90 to f90t
....
Compile t2.f90 to f90t2
....
$ python run.py       # finally ok
"This line is printed by t.f90"
"This line is printed by t2.f90 !!!!!!!!!!!!!!!!"

我不知道为什么会这样。有人可以帮忙吗?
任何建议都将不胜感激。谢谢!

解决方法

您的makefile期望构建f90tf90t2,但是正如您所说的那样

生成两个.so文件:f90t.cpython-36m-x86_64-linux-gnu.so

因此,在下次运行时,make找不到f90t,然后再次调用该命令。您可以做类似的事情

    @touch $@

在规则末尾或重命名目标。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...