看似不一致的 Makefile 行为(使用 Fortran)

Seemingly inconsistent Makefile behavior (w/ Fortran)

我在过去的一年里刚刚开始使用 Fortran 工作,这确实是我对编译语言的第一次重要体验。在工作中,我们使用管理编译和 linking 的开发环境。然而,为了让我的 Fortran 游戏顺利进行,我已经开始在家里实现数据结构和算法。因此,我是第一次进入 Makefile。

此时我只有两个程序,Linked List and the Quick Find 算法的实现。

链接列表示例之后的(非常基本的)Makefile I wrote for the Linked List program links and compiles without a hitch. I attempted to model the Quick Find Makefile,但由于某种原因它无法生成 .mod 文件。此外,如果我通过以下方式显式生成 .mod 文件...

gfortran -c QuickFindUF.f90

...Makefile 将编译并且 link 其余部分毫无怨言。我确信我犯了一个菜鸟错误,但如果有人能指出我的疏忽,我将不胜感激。

更新:为了回应评论,我添加了 makefile 的内容:

链表

# Makefile for the LinkedList test collection

# Define function to compile and link component scripts
ll_test: LinkedListTest.o LinkedList.o
    gfortran -o ll_test LinkedListTest.o LinkedList.o

# Define functions for each script that compile without linking
LinkedList.mod: LinkedList.o LinkedList.f90
    gfortran -c LinkedList.f90

LinkedList.o: LinkedList.f90
    gfortran -c LinkedList.f90

LinkedListTest.o: LinkedListTest.f90
    gfortran -c LinkedListTest.f90

# Define housekeeping function
clean:
    rm LinkedListTest.o LinkedList.o ll_test 

快速查找

# Makefile for the union_find collection

# Define function to compile and link component scripts
u_find: union_find.o QuickFindUF.o
    gfortran -o u_find union_find.o QuickFindUF.o

# Define functions for each script that compile without linking
QuickFindUF.mod: QuickFindUF.o QuickFindUF.f90
    gfortran -c QuickFindUF.f90

QuickFindUF.o: QuickFindUF.f90
    gfortran -c QuickFindUF.f90

union_find.o: union_find.f90
    gfortran -c union_find.f90

# Define housekeeping function
clean:
    rm union_find.o QuickFindUF.o u_find 

这是以下文件的顺序:

# Define function to compile and link component scripts
u_find: union_find.o QuickFindUF.o
    gfortran -o QuickFindUF.o u_find union_find.o

union_find.f90 依赖于 QuickFindUF.f90 生成的 module 但它首先编译 union_find 所以它需要的 module 还不存在。

如果您切换顺序,使其首先构建 QuickFindUF,它将起作用:

# Define function to compile and link component scripts
    u_find: QuickFindUF.o union_find.o
        gfortran -o QuickFindUF.o u_find union_find.o

但更好的做法是利用他们列出但未执行任何操作的 mod 依赖项:

QuickFindUF.mod: QuickFindUF.o QuickFindUF.f90
    gfortran -c QuickFindUF.f90

QuickFindUF.o: QuickFindUF.f90
    gfortran -c QuickFindUF.f90

union_find.o: union_find.f90 QuickFindUF.mod #Add the module dependency to union_find
    gfortran -c union_find.f90