A Makefile provides an easy, fast way to compile an application that requires more than just a simple gcc myprogram.c -o myprogram command. For example, to compile a GTK+ app, the common, basic compile command is gcc -g -Wall myguiapp.c -o myguiapp -export-dynamic `pkg-config –cflags –libs gtk+-2.0`. It’s not terrible, but it would be a pain to have to retype that every time you need to recompile. The alternative is to create a Makefile and type make. This is my very first Makefile, and I believe it makes a great example:
NAME=myguiapp CFLAGS=-g -Wall -o $(NAME) GTKFLAGS=-export-dynamic `pkg-config --cflags --libs gtk+-2.0` SRCS=main.c CC=gcc # top-level rule to create the program. all: main # compiling the source file. main: $(SRCS) $(CC) $(CFLAGS) $(SRCS) $(GTKFLAGS) # cleaning everything that can be automatically recreated with "make". clean: /bin/rm -f $(NAME)
As you can see, when make is called, it will compile the source with all my options including the program name. When make clean is called, it will delete the compiled program.
One important thing to remember is that these Makefiles are very picky about spacing and tabs. You MUST use a TAB at the beginning of commands and a TAB must not be at the beginning of blank lines. The first error causes the commands not to run. The second causes the “make” utility to complain that there is a “blank” command.
Obviously, this is an incredibly simple Makefile in comparison to a lot of them out there, but this is a good starting point. You can customize the Makefile to match your compiling needs. If you’d like to learn more, Google is your friend. I can point you in a couple of good directions with these links: