Chapter 6: Additional Software Development Tools


The sections in this chapter cover several utilities that do not quite fit anywhere else. These tools come with GNOME and can save a significant amount of time and energy when building a GNOME application.

6.1 pkg-config

If you try any of the programming examples in this book, you need to compile the code with options for the libraries, include paths, and library paths. Let's say that your GNOME installation prefix is /opt/gnome . To compile a program that requires only GLib, you need to enter something like this:

 $ gcc -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include \       -o program program.c -L/opt/gnome/lib -lglib-2.0 

This requires quite a bit of typing ” but things really get out of control when you try to compile one of the GTK+ examples from Chapter 3:

 $ gcc -I/opt/gnome/include/gtk-2.0 -I/opt/gnome/lib/gtk-2.0/include \       -I/opt/gnome/include/atk-1.0 -I/opt/gnome/include/pango-1.0 \       -I/usr/X11R6/include -I/usr/include/freetype2 \       -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include \       -o program program.c \       -L/opt/gnome/lib -L/usr/X11R6/lib -lgtk-x11-2.0 -lgdk-x11-2.0 \       -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 \       -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0 

And if you think this is bad, just think of what the GNOME examples in Chapter 4 require. Even entering this information into a Makefile is too much to ask. Not only is it nearly impossible to remember the exact paths and version numbers , but the specifics vary wildly between systems and releases.

Older releases of GNOME addressed this problem with configuration shell scripts for each component. When you wanted to include a certain path in your compiler's options, you could use command substitution to send the shell script's output to your compiler command line. However, this had its own share of glitches, especially with dependencies.

In GNOME 2.0, one program named pkg-config replaces each shell script. One directory (typically $(PREFIX)/lib/pkgconfig ) contains configuration files with .pc extensions for each GNOME package. You should take a look at one of them; for example, here is gtk+-2.0.pc :

 prefix=/opt/gnome exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir=${prefix}/include target=x11 gtk_binary_version=2.2.0 gtk_host=i586-pc-linux-gnu Name: GTK+ Description: GIMP Tool Kit (${target} target) Version: 2.2.1 Requires: gdk-${target}-2.0 atk Libs: -L${libdir} -lgtk-${target}-2.0 Cflags: -I${includedir}/gtk-2.0 

Everything is there: The name, version, installation path, dependencies, compiler options, and more are all available with a single pkg-config command.

Make sure that your PKG_CONFIG_PATH contains all of the directories on your system that have .pc files. Like PATH and MANPATH , this is a colon -delimited set of directory names :

 $ echo PKG_CONFIG_PATH /usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/opt/gnome/lib/pkgconfig 

6.1.1 Package Lists, Versions, and Descriptions

If your system and environment are working correctly, you can run the following command to print a catalog of all known packages with descriptions:

 pkg-config --list-all 

All other pkg-config options require a package name. Run this to see if package exists on your system:

 $ pkg-config --exists  package  

The pkg-config command exits with status 0 if the package exists (useful for shell programming). If you want to know the exact version number, type

 $ pkg-config --modversion  package  

Here are some examples of how you might use pkg-config to check on GLib and a nonexistent package:

 $ pkg-config --exists glib-2.0 $ echo $? 0 $ pkg-config --exists glont-5.9 $ echo $? 1 $ pkg-config --modversion glib-2.0 2.2.1 $ pkg-config --modversion glont-5.9 Package glont-5.9 was not found in the pkg-config search path. Perhaps you should add the directory containing `glont-5.9.pc' to the PKG_CONFIG_PATH environment variable No package 'glont-5.9' found 
Note  

Don't confuse --modversion with --version . The --version option always returns the pkg-config version.

6.1.2 Determining Compiler and Linker Options

Although it's nice to know a package version, the real purpose of pkg-config is to determine compiler options with --cflags and linker options with --libs .

You can combine options and packages; here are some examples:

 $ pkg-config --cflags glib-2.0 -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include $ pkg-config --libs glib-2.0 -L/opt/gnome/lib -lglib-2.0 $ pkg-config --cflags --libs glib-2.0 -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include \ -L/opt/gnome/lib -lglib-2.0 $ pkg-config --cflags --libs glib-2.0 libxml-2.0 -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include \ -I/opt/gnome/include/libxml2 \ -L/opt/gnome/lib -L/opt/gnome/lib -lglib-2.0 -lxml2 -lz -lm 

Use shell command substitution to compile a single source file glib_program.c into an executable:

 gcc -o glib_program glib_program.c `pkg-config glib-2.0 --cflags --libs` 

6.1.3 Using pkg-config in a Makefile

Most software packages are too large for a single command line. Here is how you might use pkg-config in a Makefile:

 CFLAGS=-ansi -Wall `pkg-config glib-2.0 --cflags` LDFLAGS=`pkg-config glib-2.0 --libs` glib_program: glib_program.c         gcc $(CFLAGS) -o glib_program glib_program.c $(LDFLAGS) 



The Official GNOME 2 Developers Guide
The Official GNOME 2 Developers Guide
ISBN: 1593270305
EAN: 2147483647
Year: 2004
Pages: 108

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net