Building Applications in Gtk+
Gtk+ is a C-based toolkit that provides all the necessary functions and the general framework for writing graphical interfaces in UNIX, Mac OS, and Windows operating systems. Since it is completely written in C, one can use just gcc to create new applications in any of these environments.
When compiling C applications, it is necessary to tell the compiler where to find header files and libraries. The traditional way to do this is using Makefile variables. They are called CFLAGS and LDFLAGS, respectively.
Gtk+ simplifies the task of application writers by providing a utility that provides the information to be stored on CFLAGS and LDFLAGS. The tool is called gtk-config, and is part of the full installation of Gtk+.
Using gtk-config, one can get the locations where header files and libraries are stored, without having to specify these paths directly. This helps with portability, because it is not necessary to define values that are machine-specific. The gtk-config tool will provide the files no matter what the script is run.
To get the compile information from gtk-config, developers have two main command line options:
- The --cflags option returns the value that would be stored in the CFLAGS variable in a makefile. Typically, this value tells the compiler where the header files for Gtk+ are stored.
- The --libs option returns the value that would be stored in the LDFLAGS variable in a makefile. Typically, this value tells the compiler where the libraries (both static and shared object libraries) are stored for the current machine.
For an example, suppose that the file we want to compile is called hello.c. Then, we can use gtk-config in combination with the gcc compiler to create a complete application. We can use the following command line:
gcc hello.c -o hello `gtk-config --cflags` `gtk-config --libs`
This command line combines the values returned by gtk-config for both --cflags and --libs options. Note the use of back quotes to indicate that the output of gtk-config will be used as part of the command line for gcc.
Post a comment