Note: This needs to be updated to GTK+ 3. You might be better off starting from the gnome-hello example program available from the GNOME git repository: https://git.gnome.org/browse/gnome-hello/
1. Creating a GTK Project using GNU Autotools
You will need the latest versions of GNU Autotools (autoconf, automake, and libtool) installed on your machine.
1.1. Instructions for getting GNU Autotools on Ubuntu
$ sudo apt-get install autoconf autoconf-archive \ automake automake1.9 automake1.9-doc \ automake1.7 automake1.4 \ libtool libtool-doc $ autoconf --version autoconf (GNU Autoconf) 2.61 $ automake --version automake (GNU automake) 1.10 $ libtool --version ltmain.sh (GNU libtool) 1.5.22 Debian 1.5.22-4 (1.1220.2.365 2005/12/18 22:14:06)
We installed several versions of automake for a reason--many projects depend on specific versions of automake (including GTK+). Once you have these installed and set up properly, you're ready to create a project that uses the GTK+ libraries.
1.2. Creating the project
To create a GTK+-based project using GNU Autotools you will
- Create a directory structure for your project.
- Add the necessary configuration information required by GNU Autotools.
- Add your source code files.
- Generate the project build scripts.
1.2.1. Create the directory structure for your project
code$ mkdir project project$ cd project project$ mkdir src tests project$ touch NEWS AUTHORS HACKING README ChangeLog
1.2.2. Add project configuration information
This information is used by GNU Autotools to generate build files for your project. Use your favorite editor to create these files.
1.2.2.1. project/configure.ac
AC_PREREQ(2.60) AC_INIT([project], [0.1], [name@domain.com]) AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE([1.9.6 -Wall -Werror dist-bzip2]) AC_PROG_CC # Compiling sources with per-target flags requires AM_PROG_CC_C_O AM_PROG_CC_C_O AC_PROG_INSTALL AC_PROG_LIBTOOL AM_PATH_GTK_2_0([2.10.0],,AC_MSG_ERROR([Gtk+ 2.10.0 or higher required.])) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([ Makefile src/Makefile tests/Makefile ]) AC_OUTPUT
1.2.2.2. project/Makefile.am
## Process this file with automake to generate Makefile.in SUBDIRS = src tests EXTRA_DIST = HACKING CLEANFILES = *~
1.2.2.3. project/src/Makefile.am
## Process this file with automake to generate a Makefile.in ## To build all programs with GTK+ uncomment these lines. ##AM_CPPFLAGS = @GTK_CFLAGS@ ##AM_LDADD = @GTK_LIBS@ bin_PROGRAMS = gui-program cli-program gui_program_SOURCES = gui.c cli_program_SOURCES = cli.c ## Only our GUI program uses GTK+ at the moment. gui_program_CPPFLAGS = @GTK_CFLAGS@ gui_program_LDADD = @GTK_LIBS@ CLEANFILES = *~
1.2.2.4. project/tests/Makefile.am
## Process this file with automake to generate Makefile.in CLEANFILES = *~
1.2.3. Create your source files
Now you can create your source files.
1.2.3.1. project/src/gui.c
#include <gtk/gtk.h> gint main (gint argc, gchar *argv[]) { GtkWidget *window = NULL; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request (window, 400, 300); gtk_container_set_border_width (GTK_CONTAINER (window), 10); g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL); gtk_widget_show (window); gtk_main (); return 0; }
1.2.3.2. project/src/cli.c
#include <stdio.h> #include <stdlib.h> int main (void) { puts ("Sample cli program."); return EXIT_SUCCESS; }
1.2.4. Generating the project build scripts
In the top level directory of your project, (that is `project'):
project$ autoreconf --install Putting files in AC_CONFIG_AUX_DIR, `build-aux'. configure.ac:8: installing `build-aux/compile' configure.ac:4: installing `build-aux/missing' configure.ac:4: installing `build-aux/install-sh' src/Makefile.am: installing `build-aux/depcomp' Makefile.am: installing `./INSTALL' Makefile.am: installing `./COPYING' project$ ./configure --enable-maintainer-mode project$ make cd . && /bin/bash /home/me/code/project/build-aux/missing --run autoheader rm -f stamp-h1 touch config.h.in cd . && /bin/bash ./config.status config.h config.status: creating config.h config.status: config.h is unchanged make all-recursive make[1]: Entering directory `/home/me/code/project' Making all in src ... More output flies by. project$ make distcheck ... ============================================= project-0.1 archives ready for distribution: project-0.1.tar.gz project-0.1.tar.bz2 =============================================
1.2.5. Running your program
Voila! And you're ready to run your program.
project$ src/gui