2017-07-19 56 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