2010-07-18 38 views
4

我有下面的Makefile:如何縮短Makefile的使用時間?

all: hello.exe hellogtk.exe hellogtktng.cs 

hello.exe: hello.cs 
gmcs hello.cs 

hellogtk.exe: hellogtk.cs 
gmcs -pkg:gtk-sharp-2.0 hellogtk.cs 

hellogtktng.exe: hellogtktng.cs 
gmcs -pkg:gtk-sharp-2.0 hellogtktng.cs 

clean: 
rm -f *.exe 

我纔剛剛開始學習如何寫Makefile中,我覺得這一切都有點重複。 Makefile專業人員如何去做這件事?

回答

7
all: hello.exe hellogtk.exe hellogtktng.exe 

%.exe: %.cs 
gmcs -pkg:gtk-sharp-2.0 $< 

clean: 
rm -f *.exe 
+1

哇,謝謝你,它的工作原理!你能介紹一下我的文檔嗎?它看起來很嚇人。 我是否正確理解我會用gtk-sharp-2.0編譯hello.cs(不需要它)呢?我可以做這樣的事嗎? (它不這樣工作) hello.exe:%.cs gmcs $ < – theone 2010-07-18 08:46:54

+0

http://www.delorie.com/gnu/docs/make/make_toc.html – 2010-07-18 08:56:22

+0

@theone,更具體地說,定義和重新定義模式規則](http://www.gnu.org/software/make/manual/make.html#Pattern-Rules) – 2010-07-18 09:11:43

6

Here's how you can add flags to specific targets.

# An empty variable for flags (Not strictly neccessary, 
# undefined variables expand to an empty string) 
GMCSFLAGS = 

# The first target is made if you don't specify arguments 
all: hello.exe hellogtk.exe hellogtktng.exe 

# Add flags to specific files 
hellogtk.exe hellogtktng.exe: GCMSFLAGS = -pkg:gtk-sharp-2.0 

# A pattern rule to transform .cs to .exe 
# The percent sign is substituted when looking for dependancies 
%.exe:%.cs 
    gmcs $(GMCSFLAGS) $< 
# $() expands a variable, $< is the first dependancy in the list 
+0

哇,這太棒了!它和我的原始Makefile完全一樣! – theone 2010-07-18 11:22:19