makefile执行另一个目标 [英] makefile execute another target

查看:422
本文介绍了makefile执行另一个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个makefile文件,其结构如下:

I have a makefile structured something like this:

all : 
    compile executable

clean :
    rm -f *.o $(EXEC)

我意识到在运行"make all"之前,我一直在终端中先执行"make clean",然后执行"clear".在尝试筛选令人讨厌的C ++编译错误之前,我希望拥有一个干净的终端.所以我尝试添加第三个目标:

I realized that I was consistently running "make clean" followed by "clear" in my terminal before running "make all". I like to have a clean terminal before I try and sift through nasty C++ compilation errors. So I tried to add a 3rd target:

fresh :
    rm -f *.o $(EXEC)
    clear
    make all

这可以工作,但是可以运行make的第二个实例(我相信).是否有正确的方法可以在不运行make的第二个实例的情况下获得相同的功能?

This works, however this runs a second instance of make (I believe). Is there a right way to get the same functionality without running a 2nd instance of make?

推荐答案

实际上您是对的:它运行另一个make实例. 可能的解决方案是:

Actually you are right: it runs another instance of make. A possible solution would be:

.PHONY : clearscr fresh clean all

all :
    compile executable

clean :
    rm -f *.o $(EXEC)

fresh : clean clearscr all

clearscr:
    clear

通过调用make fresh,您首先获得clean目标,然后获得clearscreen(运行clear),最后是all(完成任务).

By calling make fresh you get first the clean target, then the clearscreen which runs clear and finally all which does the job.

修改8月4日

使用make的-j选项进行并行构建会发生什么情况? 有一种固定订单的方法.在制作手册的第4.2节中:

What happens in the case of parallel builds with make’s -j option? There's a way of fixing the order. From the make manual, section 4.2:

但是,有时候,您希望对要调用的规则施加特定的顺序,但如果执行了其中一个规则,则不必强制更新目标.在这种情况下,您要定义仅订购的先决条件.可以通过在先决条件列表中放置管道符号(|)来指定仅订购的先决条件.右侧的任何先决条件仅适用于订购:target:正常先决条件|仅订购先决条件

Occasionally, however, you have a situation where you want to impose a specific ordering on the rules to be invoked without forcing the target to be updated if one of those rules is executed. In that case, you want to define order-only prerequisites. Order-only prerequisites can be specified by placing a pipe symbol (|) in the prerequisites list: any prerequisites to the left of the pipe symbol are normal; any prerequisites to the right are order-only: targets : normal-prerequisites | order-only-prerequisites

正常的先决条件部分当然可以为空.同样,您仍然可以为同一目标声明多行先决条件:它们会适当地附加.请注意,如果您将同一个文件声明为普通和仅订购的前提条件,则以普通的前提条件为准(因为它们是仅订购的前提条件的行为的严格超集).

The normal prerequisites section may of course be empty. Also, you may still declare multiple lines of prerequisites for the same target: they are appended appropriately. Note that if you declare the same file to be both a normal and an order-only prerequisite, the normal prerequisite takes precedence (since they are a strict superset of the behavior of an order-only prerequisite).

因此makefile变为

Hence the makefile becomes

.PHONY : clearscr fresh clean all

all :
    compile executable

clean :
    rm -f *.o $(EXEC)

fresh : | clean clearscr all

clearscr:
    clear

修改12月5日

运行多个makefile实例并不重要,因为任务中的每个命令都是调用函数使用可重用的方法.

It is not a big deal to run more than one makefile instance since each command inside the task will be a sub-shell anyways. But you can have reusable methods using the call function.

log_success = (echo "\x1B[32m>> $1\x1B[39m")
log_error = (>&2 echo "\x1B[31m>> $1\x1B[39m" && exit 1)

install:
  @[ "$(AWS_PROFILE)" ] || $(call log_error, "AWS_PROFILE not set!")
  command1  # this line will be a subshell
  command2  # this line will be another subshell
  @command3  # Use `@` to hide the command line
  $(call log_error, "It works, yey!")

uninstall:
  @[ "$(AWS_PROFILE)" ] || $(call log_error, "AWS_PROFILE not set!")
  ....
  $(call log_error, "Nuked!")

这篇关于makefile执行另一个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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