Makefile中的多个依赖关系级别 [英] Multiple Level of Dependencies in a Makefile

查看:779
本文介绍了Makefile中的多个依赖关系级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对makefile非常陌生.通过复制在线找到的不同示例,我已经能够编写以下所示的脚本.如果我做错了或不是常规的,请指出.

I am very new to makefiles. I have been able to write the script shown below by copying different examples found online. If I am doing something wrong, or not conventional, please point it out.

这是我拥有的fortran代码的(工作中的)Makefile.存储在main.f08中的主程序调用存储在file1.f08中的模块:

This is my (working) Makefile for a fortran code I have. The main program stored in main.f08 calls a module stored in file1.f08:

FC = gfortran

SRCS: main.f08
OBJS: $(SRCS:.f08=.o)
EXEC: $(SRCS:.f08=)

all: $(EXEC)

file1.o file1.mod: file1.f08
     $(FC) -c $<
     touch $*.o $*.mod

main.o: file1.mod

%.o: %.f08
      $(FC) $(FFLAGS) -c $<

main: file1.o

%: %.o
      $(FC) -o MainExe $^

clean:
      rm -f MainExe *.o *.mod

我试图外推这种技术,以编写具有一系列依赖关系的makefile.例如,main.f08将调用模块file1.f08,该模块又将调用模块file2.f08.这是我到目前为止的内容:

I have tried to extrapolate that technique to write a makefile which has a ladder of dependencies. For example, main.f08 would be calling the module file1.f08, which in turn would be calling the module file2.f08. Here is what I have so far:

FC = gfortran

SRCS: main.f08
OBJS: $(SRCS:.f08=.o)
EXEC: $(SRCS:.f08=)

all: $(EXEC)

file2.o file2.mod: file2.f08
      $(FC) -c $<
      touch $*.o $*.mod    

file1.o: file2.mod

file1.o file1.mod: file1.f08
      $(FC) -c $<
      touch $*.o $*.mod

main.o: file2.mod file1.mod

%.o: %.f08
      $(FC) $(FFLAGS) -c $<

main: file1.o

%: %.o
      $(FC) -o MainExe $^

clean:
      rm -f MainExe *.o *.mod

命令行输出包括几行,表明代码开始编译两个模块和主程序,然后显示几行错误,如下所示:

The command line output consist of a few line showing that the code starts compiling the two modules and the main program and then several lines of errors that look like this:

Undefined symbols for architecture x86_64:
  "___brownian_MOD_calcb", referenced from:
  ___integral_MOD_calcint in Integral.o

推荐答案

我一直在努力,实际上我已经弄清楚了.

I've been working on this and I actually have figured it out.

main可执行文件应以正确的顺序链接到模块的目标文件(目标文件不应位于其依赖项之前),然后是其目标文件main.o.模块的目标文件应链接到其依赖项(如果有)及其.f08文件.最后,touch命令确保.mod文件与.o文件保持最新.这是最简单版本中显示的更正代码:

The main executable should be linked to the module's object files in the correct order (an object file should not precede its dependency) followed by its object file, main.o. The module's object file should be linked to their dependency (if any) and their .f08 file. Finally, the touch command makes sure that the .mod files are up to dates with the .o files. Here is the corrected code showed in its simplest version:

FC = gfortran

all: main

main  : file2.o file1.o main.o
       $(FC) -o executable file2.o file1.o main.o
main.o: main.f08        
       $(FC) -c main.f08

file2.o  : file2.f08
       $(FC) -c file2.f08
       touch file2.o file2.mod

file1.o  : file2.o file1.f08
       $(FC) -c file1.f08
       touch file1.o file1.mod

makefile的更高级版本如下所示:

A more advanced version of the makefile is shown below:

FC = gfortran

SRCS = main.f08
SOBJ = $(SRCS:.f08=.o)
EXEC = #(SRCS:.f08=)

FILE = file2.f08 file1.f08
FOBJ = $(SRCS:.f08=.o)

all: $(EXEC)

$(EXEC): $(FOBJ) $(SOBJ)
       $(FC) -o executable $^

%.o: %.f08
       $(FC) -c $<
       touch $*.o $*.mod

这篇关于Makefile中的多个依赖关系级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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