2017-07-19 74 views
0

我正在寻找为什么sub-make不能按预期工作的解释。我需要sub-make,因为我需要在将文件复制到目标目录之前运行一些检查。Makefile - 为什么这个子工作不起作用?

下面的作品,但我没有足够的空间复制前做我的检查:

SRC_DIR=../src/ 
TRG_DIR=../../../trg/ 

target1.PREREQUISITES = file11.sth file12.sth file13.sth 
target2.PREREQUISITES = file21.sth file22.sth file23.sth 

copyToLocalTraining : $(TRG_DIR)file21.sth 
     @echo $(SRC_DIR)file21.sth 
     @echo $(TRG_DIR)file21.sth 

$(TRG_DIR)% : $(SRC_DIR)% 
     @echo cp -fp $^ [email protected] 
     @echo ls -ltr $? [email protected] 


.IGNORE: 
.SUFFIXES: .sfx .sth 

及以下不工作:

SRC_DIR=../src/ 
TRG_DIR=../../../trg/ 

target1.PREREQUISITES = file11.sth file12.sth file13.sth 
target2.PREREQUISITES = file21.sth file22.sth file23.sth 

copyToLocalTraining : 
     @echo "My Checks Here" 
     $(MAKE) $(TRG_DIR)file21.sth 

$(TRG_DIR)% : $(SRC_DIR)% 
     @echo cp -fp $^ [email protected] 
     @echo ls -ltr $? [email protected] 


.IGNORE: 
.SUFFIXES: .sfx .sth 

没有规则,使目标../../../trg/file21.sth

我介绍了一个中间步骤,看看我是否能够正常工作,但它不会:

SRC_DIR=../src/ 
TRG_DIR=../../../trg/ 

target1.PREREQUISITES = file11.sth file12.sth file13.sth 
target2.PREREQUISITES = file21.sth file22.sth file23.sth 

copyToLocalTraining : 
     @echo "My Checks Here" 
     $(MAKE) intermediateStep 

intermediateStep : $(TRG_DIR)file21.sth 

$(TRG_DIR)% : $(SRC_DIR)% 
     @echo cp -fp $^ [email protected] 
     @echo ls -ltr $? [email protected] 


.IGNORE: 
.SUFFIXES: .sfx .sth 

我得到:没有规则,使目标intermediateStep

+0

我无法复制您的结果;你的第二个makefile似乎工作。我建议你检查一下以确保'../ src/file21.sth'存在,然后用各种makefile尝试'make ../../../ trg/file21.sth'。 – Beta

+0

同样在这里,不能重现这一点。它是GNU吗?你只有一个'Makefile'吗?没有'makefile'? –

+0

我的makefile实际上叫做'maketest',我把它叫做'make -f maketest'。我在同一个目录中有另一个'make site',但我并不期望他们会干涉。 source ** file21.sth确实存在**,并确认这是第一个生成文件块在这里张贴的作品。我将在我的iMac中尝试一下,看它是否表现相同。 –

回答

0

我找到了原因。我的测试文件名为maketest1maketest2maketest3。我错误地认为$(MAKE)在默认情况下正在对其自身进行递归调用,但实际上它正在寻找名为makefile的文件。为了解决这个问题,我必须获得当前makefile的名称并将其作为-f参数传递给$(MAKE)。为了得到名字,我现在正在设置一个新变量,我在我的测试makefile的开头调用THISMAKEFILE,然后执行任何include指令并将它传递给$(MAKE)。所以最终版本如下。请注意,该作业是:=而不是=

THISMAKEFILE:=$(lastword $(MAKEFILE_LIST)) 

SRC_DIR=../src/ 
TRG_DIR=../../../trg/ 

target1.PREREQUISITES = file11.sth file12.sth file13.sth 
target2.PREREQUISITES = file21.sth file22.sth file23.sth 

copyToLocalTraining : 
     @echo "My Checks Here" 
     $(MAKE) -f $(THISMAKEFILE) $(TRG_DIR)file21.sth 

$(TRG_DIR)% : $(SRC_DIR)% 
     @echo cp -fp $^ [email protected] 
     @echo ls -ltr $? [email protected] 


.IGNORE: 
.SUFFIXES: .sfx .sth