首页 > 解决方案 > 使用 makefile 编译具有不同文件夹中的源文件的程序

问题描述

所以我一直在尝试使用这个已发布问题的答案和想法:Makefile: Compiling from directory to another directory

在以下摘录代码中,您可以看到目录的定义,用于源和编译对象。

all : Mpois Mkvz MBJ Mlid

# Definitions
COMPILER := gfortran -O3
LIBS := -g -fbounds-check -ffast-math -lm

# Directories of object code
OBJDIR = objects
SRCDIR = src

SOURCES := $(SRCDIR)/lbm_const.f90 $(SRCDIR)/BORDERS.f90 $(SRCDIR)/CONVERGENCE.f90 $(SRCDIR)/FILESIO.f90 $(SRCDIR)/LBM.f90
OBJECTS := $(OBJDIR)/lbm_const.o $(OBJDIR)/BORDERS.o $(OBJDIR)/CONVERGENCE.o $(OBJDIR)/FILESIO.o $(OBJDIR)/LBM.o

SOURCES_pois := $(SRCDIR)/Main_pois.f90
OBJECTS_pois := $(OBJDIR)/Main_pois.o

#   Linking
Mpois: $(OBJECTS) $(OBJECTS_pois)
    $(COMPILER) $^ -o $@ $(LIBS)

#   Compiling
$(OBJECTS): $(OBJDIR)/%.o: %.f90
    $(COMPILER) -c $< -o $@

#   Compiling
$(OBJECTS_pois): $(OBJDIR)/%.o: %.f90
    $(COMPILER) -c $< -o $@

clean:
    rm -f $(OBJDIR)/*.o
    rm -f $(OBJDIR)/*.mod
    rm -f $(SRCDIR)/*.mod

运行 makefile 脚本时出现以下错误:

make: *** No rule to make target 'lbm_const.f90', needed by 'objects/lbm_const.o'.  Stop.

有趣的是,当对 makefile 的SRCDIR = src更改SRCDIR = .编译时,即使文件夹 src 中有文件。

标签: makefilecompiler-errors

解决方案


推荐阅读