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/

Creating a GTK Project using GNU Autotools

You will need the latest versions of GNU Autotools (autoconf, automake, and libtool) installed on your machine.

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.

Creating the project

To create a GTK+-based project using GNU Autotools you will

  1. Create a directory structure for your project.
  2. Add the necessary configuration information required by GNU Autotools.
  3. Add your source code files.
  4. Generate the project build scripts.

Create the directory structure for your project

code$ mkdir project
project$ cd project
project$ mkdir src tests
project$ touch NEWS AUTHORS HACKING README ChangeLog

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.

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

project/Makefile.am

## Process this file with automake to generate Makefile.in
SUBDIRS = src tests
EXTRA_DIST = HACKING
CLEANFILES = *~

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 = *~

project/tests/Makefile.am

## Process this file with automake to generate Makefile.in
CLEANFILES = *~

Create your source files

Now you can create your source files.

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;
}

project/src/cli.c

#include <stdio.h>
#include <stdlib.h>

int
main (void)
{
  puts ("Sample cli program.");
  return EXIT_SUCCESS;
}

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
=============================================

Running your program

Voila! And you're ready to run your program.

project$ src/gui

Attic/Create a GTK Project using autotools (last edited 2013-11-25 16:38:02 by WilliamJonMcCann)