2010-07-18 5 views
4

J'ai le Makefile suivant:Comment rendre un Makefile plus court?

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 

Je commence seulement à apprendre à écrire Makefile, et je pense que tout cela est un peu répétitif. Comment les professionnels de Makefile vont-ils faire cela?

Répondre

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

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

clean: 
rm -f *.exe 
+1

Wow, merci, ça marche! Pourriez-vous me diriger vers la documentation à ce sujet? Il semble intimement perl-y. Est-ce que je comprends bien que je compilerais hello.cs avec gtk-sharp-2.0 (qui ne l'exige pas) alors? Puis-je faire quelque chose comme ça? (ça ne fonctionne pas comme ça) hello.exe:% .cs gmcs $ < – theone

+0

http://www.delorie.com/gnu/docs/make/make_toc.html –

+0

@theone, plus précisément, [ Définir et redéfinir les règles de modèle] (http://www.gnu.org/software/make/manual/make.html#Pattern-Rules) section –

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

Wow, c'est génial! Il fait exactement la même chose que mon Makefile original! – theone

Questions connexes