递归Make-敌还是友? [英] Recursive Make - friend or foe?

查看:73
本文介绍了递归Make-敌还是友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用(GNU)Make.我目前在每个目录中放置一个makefile,并使用SUBDIRS指定子目录. 有人向我建议,这不是使用make的理想方法,而是使用一个顶级make文件(或几个,使用include拆分).过去,我曾尝试过迁移/使用这种布局,但是在我看来,这并不必要的复杂.

I'm using (GNU) Make in my project. I'm currently putting one makefile per directory and specify the subdirectories using SUBDIRS. It's been suggested to me that this is not the ideal way of using make, that using a one toplevel make file (or several, split up using include). I've tried migrating/using this layout in the past, but it appears to me that it's unnecessary complicated.

使用递归生成文件有哪些好处/缺点?

Which are the benefits/drawbacks of using recursive makefiles?

推荐答案

您应该记住的第一件事(只是为了消除任何误解)是,我们不是在谈论单个或多个Makefile.在任何情况下,在每个子目录中将makefile拆分为一个可能是个好主意.

The first thing you should keep in mind (just to eliminate any misunderstanding) is that we're not talking about a single vs. multiple makefiles. Splitting your makefile in one per subdirectory is probably a good idea in any case.

递归生成文件很糟糕,主要是因为将依赖关系树划分为几棵树.这将阻止正确表达make实例之间的依赖关系.这也会导致(部分)依赖关系树被重新计算多次,这最终是一个性能问题(尽管通常不是很大).

Recursive makefiles are bad primarily because you partition your dependency tree into several trees. This prevents dependencies between make instances from being expressed correctly. This also causes (parts of) the dependency tree to be recalculated multiple times, which is a performance issue in the end (although usually not a big one.)

要正确使用单一制作方法,您需要使用一些技巧,尤其是当您具有大量代码库时:

There are a couple of tricks you need to use in order to properly use the single-make approach, especially when you have a large code base:

首先,使用GNU make(我已经知道了). GNU make具有许多简化功能的特性,您不必担心兼容性.

First, use GNU make (you already do, I see). GNU make has a number of features which simplifies things, and you won't have to worry about compatibilities.

第二,使用特定于目标的变量值.例如,这将允许您为不同的目标使用不同的CFLAGS值,而不是强迫您在整个品牌中使用单个CFLAGS:

Second, use target-specific variable values. This will allow you to have, for example, different values of CFLAGS for different targets, instead of forcing you to have a single CFLAGS in your entire make:

 main: CFLAGS=-O2
 lib: CFLAGS=-O2 -g

第三,确保在GNU make支持的最大范围内使用VPATH/vpath.

Third, make sure you use VPATH/vpath to the full extent supported by GNU make.

您还希望确保没有多个具有相同名称的源文件. VPATH的一个局限性在于它不允许您具有特定于目标的VPATH定义,因此源文件的名称必须共存于单个"VPATH命名空间"中.

You also want to make sure that you do not have multiple source files with the same name. One limitation of VPATH is that it does not allow you to have target-specific VPATH definitions, so the names of your source files will have to co-exist in a single "VPATH namespace".

这篇关于递归Make-敌还是友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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