使用Makefile将多个源目录中的多个可执行文件编译为单个bin目录 [英] Compile multiple executables from multiple source directories to single bin directory using makefile

查看:0
本文介绍了使用Makefile将多个源目录中的多个可执行文件编译为单个bin目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我正在处理的一组程序创建一个Makefile。这些程序都是用Fortran编写的,源文件位于不同的目录中。我似乎想不出怎么才能让事情运转起来。我当前的Sumfile是

#Compiler and compiler flag variables
FCOMP=/usr/local/bin/gfortran
F_FLAGS=-O2 -fbounds-check -Wall 
F_FLAGSDB=-g -fbounds-check -Wall

#paths to libraries
COMMON_LIB=/usr/local/lib/libspc_common.a
SPICE_LIB=/usr/local/lib/spicelib.a

# Paths to directories
BIN_DIR=BIN

# Get file names of component source files
#get names of files in src1
FORT_FILES=$(wildcard ./SRC1/*.f)
#get names of files in src2 
FORTFILES+=$(wildcard ./SRC2/*.f)   
#get names of files in src3 
FORTFILES+=$(wildcard ./SRC3/*.f)   

#get file names for output
EXE_FILES=$(addprefix $(BIN_DIR),$(notdir $(patsubst %.f, % , $(FORTFILES))))

# make commands
# Set the default option to compile the library with optimization
default: all

# create all command 
all: $(EXE_FILES)
    @echo toolkit has been built with optimization

#If compiling for debugging replace the compiler flags to remove optimization and add debugging
debug: F_FLAGS=$(F_FLAGSDB)
#Run compiler with debugging flags
debug: $(EXE_FILES)
    @echo toolkit has been built with debugging

# Compile all of the source files into executables 
$(EXE_FILES): % : %.f
    $(FCOMP) $(F_FLAGS) $^ $(COMMON_LIB) $(SPICE_LIB) -o $(BIN_DIR)/$@

# install the library in /usr/local/lib
install: 
    cp -p $(BIN_DIR)* /usr/local/bin/toolkit/   

# remove executable files for a clean build
clean:
    rm $(BIN_DIR)*

我遇到的问题是,当我尝试运行make时出现以下错误:

make: *** No rule to make target `Display.f', needed by `Display'.  Stop.

我认为这是因为我丢失了源文件所在的目录。有人能帮帮我吗?我完全被困住了,不知道如何继续。

此外(这是一个关于make的更一般的问题),如果Common_Lib发生更改,是否有方法告诉make重新编译所有内容?

谢谢您的帮助!

推荐答案

假设您的源文件

SRC1/alpha.f
SRC1/beta.f
SRC2/gamma.f
SRC3/delta.f

1)这里有一个缺陷:

EXE_FILES=$(addprefix $(BIN_DIR),$(notdir $(patsubst %.f, % , $(FORTFILES))))

这将产生

BINalpha BINbeta BINgamma BINdelta

我认为您是有意的

BIN/alpha BIN/beta BIN/gamma BIN/delta

简单的解决方法:

EXE_FILES=$(addprefix $(BIN_DIR)/,$(notdir $(patsubst %.f, % , $(FORTFILES))))

2)现在查看静态模式规则:

$(EXE_FILES): % : %.f
    ...
因此,要构建BIN/alpha,make必须首先找到不存在的BIN/alpha.f。若要使其查找alpha.f,请执行以下操作:

$(EXE_FILES): $(BIN_DIR)/% : %.f
    ...

3)如何查找来源?

您可以进行一些精细的编码,以帮助记住它是在哪里找到的alpha.f,但当我们可以使用the vpath directive

时,就不需要了
vpath %.f SRC1 SRC2 SRC3
4)最后一次查看规则:

此命令:

$(FCOMP) $(F_FLAGS) $^ $(COMMON_LIB) $(SPICE_LIB) -o $(BIN_DIR)/$@

将生成例如BIN/BIN/alpha,这是愚蠢的。A non-PHONY Make rule should produce a file whose name is the target of the rule.它避免了很多麻烦。

$(FCOMP) $(F_FLAGS) $^ $(COMMON_LIB) $(SPICE_LIB) -o $@

一旦您将其完美地运行,可能会进行一些进一步的改进。

这篇关于使用Makefile将多个源目录中的多个可执行文件编译为单个bin目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆