2012-07-04 48 views
1

我試圖做一個makefile,其執行以下操作:爲什麼我的makefile目標特定變量不起作用?

  • SRC目錄
  • 放於OBJ目錄對象文件
  • 提出在二進制文件中獲取源文件bin目錄
  • 將發佈目標放入rel目錄
  • 使調試目標在DBG目錄

我遇到的第一個問題是,我的目標特定的變量似乎並沒有在這裏工作是生成文件:

# Build Directories 
src_dir=src 
obj_dir=obj 
bin_dir=bin 

cc=cl 
cc_flags= 

# create the directory for the current target. 
[email protected] -p $(@D) 

# source files 
src = MainTest.cpp 

# object files - replace .cpp on source files with .o and add the temp directory prefix 
obj = $(addprefix $(obj_dir)/$(cfg_dir)/, $(addsuffix .obj, $(basename $(src)))) 

release: cfg_dir = rel 
release: executable 

debug: cfg_dir = dbg 
debug: cc_flags += -Yd -ZI 
debug: executable 

executable: $(bin_dir)/$(cfg_dir)/MainTest.exe 

# build TwoDee.exe from all of the object files. 
$(bin_dir)/$(cfg_dir)/MainTest.exe : $(obj) 
    $(dir_guard) 
    $(cc) -out:[email protected] $(obj) -link 

# build all of the object files in the temp directory from their corresponding cpp files. 
$(obj_dir)/$(cfg_dir)/%.obj : $(source_dir)/%.cpp 
    $(dir_guard) 
    $(cc) $(cc_flags) -Fo$(obj_dir)/$(cfg_dir) -c $< 

當我運行make調試,我得到:

make: *** No rule to make target `obj//MainTest.obj', needed by `bin//MainTest.exe'. 

還有別的東西錯了,因爲如果我刪除了調試和發佈變量和硬編碼cfg_dir到REL然後我得到:

make: *** No rule to make target `obj/rel/MainTest.obj', needed by `bin/rel/MainTest.exe'. Stop. 

所以我的對象規則也一定是錯誤的。如果有人看到其他錯誤的信息,我很樂意提供檔案,歡迎提出意見。

回答

1

特定於目標的變量只能在配方中使用,而不能在規則中使用。這是因爲在讀取Makefile時解析規則,並從中推導出依賴關係。

爲了讓您的規則適用於多種可能的值cfg_dir,我建議您查看GNU Make手冊的eval部分中的示例。這解釋了這樣的事情:

release: $(bin_dir)/rel/MainTest.exe 

debug: cc_flags += -Yd -ZI 
debug: $(bin_dir)/dbg/MainTest.exe 

define template = 

# build TwoDee.exe from all of the object files. 
$(bin_dir)/$(cfg_dir)/MainTest.exe : $(obj) 
    $$(dir_guard) 
    $$(cc) -out:[email protected] $(obj) -link 

# build all of the object files in the temp directory from their corresponding cpp files. 
$(obj): $(obj_dir)/$(cfg_dir)/%.obj : $$(src_dir)/%.cpp 
    $$(dir_guard) 
    $$(cc) $$(cc_flags) -Fo$(obj_dir)/$(cfg_dir) -c $$< 

endef 
$(foreach cfg_dir,rel dbg,$(eval $(template))) 
+0

這很有道理,我會嘗試使用eval。你知道爲什麼我的obj規則不起作用,即使我使用非目標特定的變量嗎? – Farnk

+0

我試圖使用eval,但它似乎並沒有工作。有什麼方法可以調試嗎? – Farnk

+0

你應該列出你的模式規則適用的所有文件:在上面做的前面加上'$(obj):'這個規則。另外請確保不要混合使用'src_dir'和'source_dir'。通過這些修改,並針對gcc及其不同選項進行調整,這些工作對我來說都很有用。 – MvG

相關問題