tags:

views:

46

answers:

2

Hi there,

I've got the following 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

I'm only starting to learn how to write Makefiles, and I feel that all this is a bit repetitive. How would Makefile pros go about doing this?

+5  A: 
all: hello.exe hellogtk.exe hellogtktng.exe

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

clean:
 rm -f *.exe
Forrest
Wow, thank you, it works! Would you mind pointing me to documentation about this? It looks intimidatingly perl-y.Do I understand it correctly that I would compile hello.cs with gtk-sharp-2.0 (which doesn't require it) then? Can I do something like this? (it doesn't work like that)hello.exe: %.cs gmcs $<
theone
http://www.delorie.com/gnu/docs/make/make_toc.html
Alex Farber
@theone, more specifically, [Defining and Redefining Pattern Rules](http://www.gnu.org/software/make/manual/make.html#Pattern-Rules) section
Pavel Shved
Thanks a bunch!
theone
+2  A: 

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
Scott Wales
Wow, this is awesome! It does exactly the same as my original Makefile!
theone