简体   繁体   English

在哪种情况下,我应该在Makefile中保留多个目标而不是一个?

[英]In which cases should I keep multiple targets instead of one in a Makefile?

targetOne : header/headerAB.h source/sourceA.cpp source/sourceB.cpp source/main.cpp
         g++ -Wall header/headerAB.h source/sourceA.cpp source/sourceB.cpp source/main.cpp

Is there a reason why I should break this in multiple targets rather then keeping it as one? 我有理由在多个目标中打破这个而不是将其保持为一个目标吗?
In what cases do we need to have separate targets rather than one in a Makefile? 在什么情况下我们需要在Makefile中使用单独的目标而不是一个?

Citing http://mrbook.org/tutorials/make/ 引用http://mrbook.org/tutorials/make/

Using dependencies. 使用依赖项。 Sometimes is useful to use different targets. 有时使用不同的目标很有用。 This is because if you modify a single file in your project, you don't have to recompile everything, only what you modified. 这是因为如果修改项目中的单个文件,则不必重新编译所有内容,只需重新编译修改后的内容。

So you will get faster recompilation after partial source update. 因此,在部分源更新后,您将获得更快的重新编译。 And this is "must to implement" for huge projects, where any change is very small (comparing to entire > 1M LOC project). 对于大型项目来说,这是“必须实施”,任何变化都非常小(与整个> 1M LOC项目相比)。

Also, multiple targets allows you to set different rules for different parts of project. 此外,多个目标允许您为项目的不同部分设置不同的规则。

Generally, for flexibility and scalability you should ask 'what is the advantage of building multiple files in the same rule'. 通常,为了灵活性和可伸缩性,您应该问“在同一规则中构建多个文件的优势是什么”。 It is probable that the first time you do a build, it's going to be quicker if everything gets compiled with the same rule. 很可能第一次进行构建时,如果所有内容都使用相同的规则进行编译,则会更快。

However on subsequent steps, if you just alter one source code file do you really want all the other source code files built to object files? 但是在后续步骤中,如果您只是更改一个源代码文件,您是否真的希望将所有其他源代码文件构建到目标文件中? I'd imagine not. 我想象不到。 But if you have it all done in the one rule, that's what'll happen. 但如果你把这一切都完成在一条规则中,那就是将要发生的事情。 And that will be expensive if you have a lot of source code files. 如果你有很多源代码文件,这将是昂贵的。

This also means if you're building libraries, you'll want to build the library as a separate step, rather than saying having one rule that compiles all the sources and then archives the library. 这也意味着如果您正在构建库,您将需要将库构建为一个单独的步骤,而不是说有一个规则编译所有源,然后归档库。 Of course, in that circumstance, it means that the library archive step doesn't need to know how the source code was compiled, or even what language it was in, which makes for easier maintenance. 当然,在这种情况下,这意味着库归档步骤不需要知道源代码是如何编译的,甚至不需要知道它是什么语言,这使得维护更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM