My First Makefile for Compiling GTK+ (Or Any) Apps

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:

CFLAGS=-g -Wall -o $(NAME)
GTKFLAGS=-export-dynamic `pkg-config --cflags --libs gtk+-2.0`

# top-level rule to create the program.
all: main

# compiling the source file.
main: $(SRCS)

# cleaning everything that can be automatically recreated with "make".
 /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:

Happy coding!

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:

There's 3 Comments So Far

  • nelson
    January 18th, 2011 at 5:46 pm

    Nice work man. This has been helpful. Regards.

  • mike
    February 26th, 2012 at 8:14 pm

    Thank you very much. You just saved my ass.

  • José
    June 27th, 2014 at 9:37 am

    Tank You very much, I try using it and works fine. I ask You about how did you know using flag “-export-dynamic”, because I didn’t get any reference searching this flag on the web. José

Share your thoughts, leave a comment!