2010-03-23 135 views

回答

24

就像从一个makefile调用任何其他命令:

target: prerequisites 
    shell_script arg1 arg2 arg3 

关于你进一步的解释:

.PHONY: do_script 

do_script: 
    shell_script arg1 arg2 arg3 

prerequisites: do_script 

target: prerequisites 
+0

但我需要在先决条件 – 2010-03-23 23:47:45

+0

@Matthew之前运行脚本,那么您的makefile设置不正确。制作一个新的'.PHONY'目标,以满足您的先决条件,并在该目标中运行该脚本。 – 2010-03-24 00:15:57

+0

为您的案例编辑更多。 – 2010-03-24 00:17:19

5

每个在makefile规则的行动是,将在执行的命令子shell。您需要确保每个命令都是独立的,因为每个命令都将在单独的子shell中运行。

出于这个原因,你会经常看到换行符逃脱当笔者想要几个命令在同一子shell中运行:

targetfoo: 
     command_the_first foo bar baz 
     command_the_second wibble wobble warble 
     command_the_third which is rather too long \ 
      to fit on a single line so \ 
      intervening line breaks are escaped 
     command_the_fourth spam eggs beans 
0

也许不是“正确”的方式做到像已经提供的答案,但是我遇到了这个问题,因为我想让我的makefile运行我编写的脚本来生成一个头文件,该文件将为整个软件包提供版本。我在这个软件包中有很多目标,并且不想为它们添加全新的先决条件。把它放在我的makefile的开头为我工作

$(shell ./genVer.sh) 

它告诉make只是运行一个shell命令。 ./genVer.sh是要运行的脚本的路径(与makefile相同的目录)和名称。无论我指定哪个目标(包括clean,这都是缺点,但最终对我来说不是一个大问题),它都会运行。