Writing Free Software – Part 2: Makefiles


Okay, so now we know how to write hello world in C#, compile it and run it. Next, let’s try to automate the build portion a bit. It’s not going to be very much of an improvement to start out with; we’re just replacing the gmcs command with a make command. But it’s laying a framework, so let’s ignore that for the time being.

Get back to the workspace

$ cd ~/src/greeting

Create a simple Makefile

$ cat > Makefile
# the first target is the one executed when no target is named
all: Greeting.exe
#don't do anything

# running 'make clean' will execute this target
clean:
	rm Greeting.exe

# running 'make Greeting.exe' will execute this target
Greeting.exe: Greeting.cs
# the same as gmcs -out:Greeting.exe Greeting.cs
	gmcs -out:$@ $^

Test the Makefile

$ make
gmcs -out:Greeting.exe Greeting.cs
$ make clean
rm Greeting.exe
$ make Greeting.exe
gmcs -out:Greeting.exe Greeting.cs
$ ./Greeting.exe 
Greetings, World!

Conclusion

So, there’s a quick overview of writing a Makefile. If you want to learn all of the nitty-gritty details, I recommend reading the manual over at gnu.org.

We’ll use these short lessons as building blocks for larger tasks, like creating a distribution using automake and autoconf. Baby steps first :)

, , , , , ,

Leave a Reply