by June 7, 2004 0 comments



A lot of software that gets distributed under the GNU public license is bundled with the source code. If you’re using a platform other than the one that the software is meant for, you can compile the source code. This can be a complex process and requires a standard to be followed. Currently, it involves using a standard set of shell scripts and macros that can be used in any case, irrespective of the platform, whether Unix or Linux distributions, libraries or compilers. To help you understand this better, we’ll talk about how to package a simple GTK+ based GNU application.

Compiling and installing a Linux/Unix-based program is usually a two-step process: configuring and building. The configuration step uses a script called configure.in. To generate this script the developer has to write a file called configure.ac. Then, you need to use a tool called Autoconf, which when run against configure.ac, gives rise to the configure.in script. This script is used to generate ‘Makefile’ that is required in the building step. The building step uses a program called Make, which reads the Makefile: which essentially has rules to build the program. Now, to generate this Makefile, the configure script requires a file called Makefile.in as input. However, writing a Makefile.in for the configure script is tedious and complex. Here is where another handy tool called Automake comes into picture. Automake automatically generates Makefile.in from a file called Makefile.am, which is easier to compose. The developer writes the file named Makefile.am according to some rules.

Direct
Hit!
Applies to: Developers
USP: Automatic generation of Makefile
Links:
www.gnu.org

Makefile.am describes the file used to build a program and certain variables. It also has special macros and target variables. Automake also adds its own rules required by Makefile. When executed, Automake first reads configure.in file generated by Autoconf and converts every AC_SUBST: used for setting up some compilation and link flags: variable into Makefile variable so that it can be referred in Makefile.am. It then reads Makefile.am to generate Makefile. in. 

Now, let’s build a GUI application using GTK+ in PCQLinux 2004. GTK+ is a library for creating GUI applications in GNU/Linux. As step one, write the C program as shown and save it as main.c in
~/gtk_ graphics/hello/src. 

#include <config.h>
#include <gtk/gtk.h>
static void on_destroy (GtkWidget * widget, gpointer data)

gtk_main_quit ();
}
int win(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *label;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (window), 30);
gtk_window_set_title (GTK_WINDOW (window), PACKAGE “ “ VERSION);
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_set_default_icon_from_file (PIXMAPS_DIR “/hello-icon.gif”,null);
on _destroy function gets called*/ 
g_signal_connect (G_OBJECT (window), “destroy”, G_CALLBACK (on_destroy), NULL);
label = gtk_label_new (“Welcome to gtk”);
gtk_container_add (GTK_CONTAINER (window), label);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}

You need to create this directory structure. You also have to create pixmaps directory in hello. Thus, hello directory will have src and pixmaps directory. This pixmap holds the image used by the application. The program consists of two main functions: on_destroy and win. GTK+ calls it the on_destroy function, when you close the windows of the application. The win function creates the windows and sets its attributes through standard window function in GTK+. The config.h file is included, which is based on the directives placed in the configure.ac file, and any user input supplied to the configure script.

In step two, save configure.ac file in ~/gtk_graphics/hello/configure. ac. The file defines the number of macros used by Autoconf and Automake to create the files for compilation. Macros that start with ‘AC_’ are for Autoconf and those that start with ‘AM_’ are for
Automake.

AC_INIT(src/main.c)
AM_CONFIG_HEADER(config.h)
PACKAGE=hello
VERSION=1
AM_INIT_AUTOMAKE($PACKAGE,$VERSION)
AC_PROG_CC
AC_PROG_INSTALL
AC_STDC_HEADERS
PKG_CHECK_MODULES(DEPS, gtk+-2.0 >= 2.2 glib-2.0 >= 2.2)
AC_SUBST(DEPS_CFLAGS)
AC_SUBST(DEPS_LIBS)
AC_OUTPUT{[
Makefile
src/Makefile
pixmaps/Makefile]}

Now we save Makefile.am in ~/gtk_graphics/hello/Makefile.am.

SUBDIRS = src\pixmaps. 

This file lists the sub-directories that contain the elements required for the Linux icon. Next, save Makefile.am in ~/gtk_graphics/hello/src/ Makefile.am. This file lists the C program and the name of the binary file to be created. It also links the library files needed for the code.

bin_PROGRAMS = hello 
hello_SOURCES = main.c
hello_LDADD = $(DEPS_LIBS)
AM_CPPFLAGS = $(DEPS_CFLAGS) \ -DPIXMAPS_DIR=\””$(datadir)/pixmaps”\”

Next, save Makefile.am in ~/gtk_graphics/hello/pixmaps/Makefile.am. This is the final Makefile to be created for pixmaps directory. The thing to remember is to specify the same destination for the icon as the one in the
src/makefile.am.

pixmapsdir = $(datadir)/pixmaps
pixmaps_DATA = hello-icon.png
EXTRA_DIST = $(pixmaps_DATA)

Now, run the following commands to package the codes.

$ cd ~/gtk_graphics/hello
$ aclocal

This creates aclocal.m4 that defines all the macros required by ‘configure.in’ file.

$ autoheader 

This reads configure.ac and aclocal.m4 to create config.h.in, which is used to create
config.h. 

$ touch stamp-h

This generates the file, stamp-h. This file ensures that Automake generates config.h from
config.h.in.

$ autoconf

This generates the configure script (configure.in file).

$ automake –a –c

This generates Makefile.in files.

$ ./configure –prefix=${HOME}/test
$ make
$ make install

The –prefix option installs the program under home directory. Then the test directory will be created in root that has a bin directory having the hello program. The share directory has the pixmaps directory with hello-icon.png icon. To run the program:

$ PATH=${HOME}/test/bin:${PATH} hello 

Sushil Oswal

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.