Programming and Graphics

(Kiana) #1

284 Introduction to C++ Programming and Graphics


To develop an application, we must link the source code with theGtk+
header and object files, which is easier said than done. Fortunately, a fabulous
application is available to help us through the linking process.


pkg-configis a public domain multi-platform application useful for com-
piling comprehensive codes that require a multitude of system libraries (see
http://pkgconfig.freedesktop.org/wiki)..) The application inserts appro-
priate compiler options in the compilation line, thereby saving us from the
painstaking task of manually citing all necessary header files and associated
libraries.


For example, to compile the programhorses.cc, we issuein a single line
the command:


c++ -o horses horses.cc ‘pkg-config --cflags gtk+-2.0‘ \
‘pkg-config --libs gtk+-2.0‘

where the backslash in the first line is a line-continuation mark.



  • The first directive ‘pkg-config –cflags gtk+-2.0’ runspkg-configto list
    the header files of thegtk+-2.0library.

  • The second directive ‘pkg-config –libs gtk+-2.0’ runspkg-configto list
    the implementations of thegtk+-2.0library.

  • The executablepkg-configitself is located in a system directory.


To installpkg-configon a Linux system, we download it from the In-
ternet sitehttp://pkgconfig.freedesktop.org/wiki, and follow the instruc-
tions, which prescribe issuing the command./configure, followed by the com-
mandmake install. The latter executes theinstallprotocol described in the
makefile. A great deal of gratitude is due to the authors of this truly useful
application. configureis ashell script, that is, a program written in one of
the Unix interpreted languages associated with a Unix shell, as discussed in
Appendix A.


The following C++ code contained in the filehorses.ccgenerates a win-
dow, displays a button, and prints the names of two horses on the button.
When the button is clicked, the window disappears.


#include <gtk/gtk.h>
using namespace std;

int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
Free download pdf