![](/img/trans.png)
[英]How to put .o and .exe files in a /build/ directory using makefile
[英]Makefile Build Directory and dependencies list
在makefile中,我在構建目錄中構建了所有.o文件:
program: class1.o class2.o class3.o
g++ $(BUILDDIR)class1.o $(BUILDDIR)class2.o $(BUILDDIR)class3.o -o $@
從依賴項列表生成$(BUILDDIR)class1.o $(BUILDDIR)class2.o $(BUILDDIR)class3.o
...
我知道$^
會給我所有依賴項的列表,用空格分隔,但我不能處理子目錄。
可能嗎 ?
如果我有program: class1.o class2.o class3.o configure
,我configure
從列表中排除configure
嗎?
謝謝 :)
編輯:邁克爾的解決方案效果很好,但是這樣做, make
找不到依賴關系並且必須每次構建所有內容...當制作類似program: class1.o class2.o class3.o
隱式規則時,不是更簡單的方法program: class1.o class2.o class3.o
,告訴它將二進制文件放在構建目錄中?
你已經遇到了兩個(或者三個)Make的大缺點。 to make files , but not the other way around, and it gets confused if a rule makes something other than the exact target (and it's automatic variables should work in prerequisites, but they don't). 這是善於利用文件在提出的文件,而不是反過來,如果一個規則使較確切的目標以外的東西它會很困惑(和它的自動變量應該在前提條件工作,但他們不這樣做)。 幸運的是,我們可以解決這些問題。
當您使用Michael Kohne的解決方案時,Make無法找到先決條件,因為它正在尋找class1.o,而不是$(BUILDDIR)class1.o。 (如果你只是簡單地告訴它使用VPATH查看$(BUILDDIR),你可能會遇到其他問題。)
最簡單,最原始的解決方案(我建議,如果你沒有太經驗)是蠻力:
NAMES = class1 class2 OBJECTS = $(patsubst %,$(BUILDDIR)%.o,$(NAMES)) program: $(OBJECTS) configure g++ $(OBJECTS) -o $@ $(OBJECTS):$(BUILDDIR)%.o:%.cc g++ -c $^ -o $@
使用更先進的技術有更優雅的解決方案,但現在應該這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.