
This tutorial will demonstrate how to:
create a small "Hello, World" application using GTK+
make the .desktop file
how to set up the build system
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Hello World");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);
gtk_widget_show_all (window);
}GtkApplication initializes GTK+. It also connects the x button that's automatically generated along with the window to the "destroy" signal.
We can start building our first window. We do this by creating a variable called window and assigning it a gtk_application_window_new
The window title can be any string you want it to be. To be on the safe side, it's best to stick to UTF-8 encoding.
Now we have a window which has a title and a working "close" button. Let's add the actual "Hello World" text.
GtkWidget *label;
label = gtk_label_new ("Hello World!");
gtk_container_add (GTK_CONTAINER (window), label);Finally, we create and run the application:
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new (NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}GtkApplicationWindow can only hold one widget at a time. To construct more elaborate programs you need to create a holder widget like GtkGrid inside the window, and then add all the other widgets to it.
The complete file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *label;
window = gtk_application_window_new (app);
label = gtk_label_new ("Hello GNOME!");
gtk_container_add (GTK_CONTAINER (window), label);
gtk_window_set_title (GTK_WINDOW (window), "Welcome to GNOME");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new (NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
To run this application, first save it as hello-world.c. Then open Terminal, go to the folder where your application is stored.
Compile the program:
gcc hello-world.c `pkg-config --cflags --libs gtk+-3.0` -o hello-world
Run the program:
./hello-worldRunning applications from the Terminal is useful at the beginning of the application making process. To have fully working application integration in GNOME 3 requires a desktop launcher. For this you need to create a .desktop file. The .desktop file describes the application name, the used icon and various integration bits. A deeper insight into the .desktop file can be found here. The .desktop.in file will create the .desktop.
The example shows you the minimum requirements for a .desktop.in file.
1 2 3 4 5 6 7 8 9 10 11
[Desktop Entry] Version=1.0 Encoding=UTF-8 Name=Hello World Comment=Say Hello Exec=@prefix@/bin/hello-world Icon=application-default-icon Terminal=false Type=Application StartupNotify=true Categories=GNOME;GTK;Utility;
Now let's go through some parts of the .desktop.in file.
The application name.
A short description of the application.
Specifies a command to execute when you choose the application from the menu. In this example exec just tells where to find the hello-world file and the file takes care of the rest.
Specifies whether the command in the Exec key runs in a terminal window.
To put your application into the appropriate category, you need to add the necessary categories to the Categories line. More information on the different categories can be found in the menu specification.
In this example we use an existing icon. For a custom icon you need to have a .svg file of your icon, stored in /usr/share/icons/hicolor/scalable/apps. Write the name of your icon file to the .desktop.in file, on line 7. More information on icons in: Installing Icons for Themes and on freedesktop.org: Specifications/icon-theme-spec.
To make your application truly a part of the GNOME 3 system you need to install it with the help of autotools. The autotools build will install all the necessary files to all the right places.
For this you need to have the following files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/sh set -e test -n "$srcdir" || srcdir=`dirname "$0"` test -n "$srcdir" || srcdir=. olddir=`pwd` cd "$srcdir" # This will run autoconf, automake, etc. for us autoreconf --force --install cd "$olddir" if test -z "$NOCONFIGURE"; then "$srcdir"/configure "$@" fi
After the autogen.sh file is ready and saved, run:
$ chmod +x autogen.sh
1 2 3 4 5 6 7 8 9 10
# The actual runnable program is set to the SCRIPTS primitive. # # Prefix bin_ tells where to copy this bin_PROGRAMS = hello-world hello_world_CFLAGS = $(gtk_CFLAGS) hello_world_LDADD = $(gtk_LIBS) hello_world_SOURCES = hello-world.c desktopdir = $(datadir)/applications desktop_DATA = \ hello-world.desktop
1 2 3 4 5 6 7 8 9
# This file is processed by autoconf to create a configure script AC_INIT([Hello World], 1.0) AM_INIT_AUTOMAKE([1.10 no-define foreign dist-xz no-dist-gzip]) AC_PROG_CC AM_PROG_VALAC([0.16]) PKG_CHECK_MODULES(gtk, gtk+-3.0) AC_CONFIG_FILES([Makefile hello-world.desktop]) AC_OUTPUT
Information users should read first. This file can be blank.
When you have the hello-world.c, hello-world.desktop.in, Makefile.am, configure.ac and autogen.sh files with correct information and rights, the README file can include the following instructions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
To build and install this program: ./autogen.sh --prefix=/home/your_username/.local make make install ------------- Running the first line above creates the following files: aclocal.m4 autom4te.cache config.log config.status configure depcomp hello-world hello-world.desktop hello_world-hello-world.o install-sh missing Makefile.in Makefile Running "make" links all the appropriate libraries. Running "make install", installs the application in /home/your_username/.local/bin and installs the hello-world.desktop file in /home/your_username/.local/share/applications You can now run the application by typing "Hello World" in the Overview. ---------------- To uninstall, type: make uninstall ---------------- To create a tarball type: make distcheck This will create hello-world-1.0.tar.xz
Got a comment? Spotted an error? Found the instructions unclear? Send feedback about this page.