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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
<?xml version="1.0" encoding="utf-8"?>
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="hello-world.c" xml:lang="fr">
<info>
<title type="text">Hello World (C)</title>
<link type="guide" xref="beginner.c#tutorials" group="#first"/>
<revision version="0.1" date="2013-06-17" status="review"/>
<credit type="author copyright">
<name>Susanna Huhtanen</name>
<email its:translate="no">ihmis.suski@gmail.com</email>
<years>2012</years>
</credit>
<credit type="editor">
<name>Tiffany Antopolski</name>
<email its:translate="no">tiffany.antopolski@gmail.com</email>
</credit>
<desc>Une application basique « hello, world »</desc>
</info>
<title>How to build, install and create a <file>tar.xz</file> of a Hello World program</title>
<media type="image" mime="image/png" style="floatend" src="media/hello-world.png"/>
<synopsis>
<p>This tutorial will demonstrate how to:</p>
<list style="numbered">
<item><p>create a small "Hello, World" application using GTK+</p></item>
<item><p>make the <file>.desktop</file> file</p></item>
<item><p>how to set up the build system</p></item>
</list>
</synopsis>
<links type="section"/>
<section id="hello-world"><title>Create the program</title>
<links type="section"/>
<section id="imports"><title>Bibliothèques à importer</title>
<code mime="text/x-csrc"><![CDATA[#include <gtk/gtk.h>
]]></code>
</section>
<section id="application"><title>Création de la fenêtre principale de l'application</title>
<code mime="text/x-csrc"><![CDATA[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);
}
]]></code>
<p>GtkApplication initializes GTK+. It also connects the <gui>x</gui> button that's automatically generated along with the window to the "destroy" signal.</p>
<p>We can start building our first window. We do this by creating a variable called <var>window</var> and assigning it a gtk_application_window_new</p>
<p>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.</p>
<p>Now we have a window which has a title and a working "close" button. Let's add the actual "Hello World" text.</p>
</section>
<section id="label"><title>Étiquette de la fenêtre</title>
<code mime="text/x-csrc"><![CDATA[GtkWidget *label;
label = gtk_label_new ("Hello World!");
gtk_container_add (GTK_CONTAINER (window), label);
]]></code>
<p>Finally, we create and run the application:</p>
<code mime="text/x-csrc"><![CDATA[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;
}]]></code>
<p>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.</p>
</section>
<section id="c"><title>hello-world.c</title>
<p>The complete file:</p>
<code mime="text/x-csrc" style="numbered">#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;
}
</code>
</section>
<section id="terminal"><title>Running the application from terminal</title>
<p>To run this application, first save it as hello-world.c. Then open Terminal, go to the folder where your application is stored.</p>
<p>Compile the program:</p>
<screen>gcc <var>hello-world</var>.c `pkg-config --cflags --libs gtk+-3.0` -o <var>hello-world</var></screen>
<p>Run the program:</p>
<screen>./<var>hello-world</var></screen>
</section>
</section>
<section id="desktop.in"><title>The <file>.desktop.in</file> file</title>
<p>Running applications from the Terminal is useful at the beginning of the application making process. To have fully working <link href="https://developer.gnome.org/integration-guide/stable/mime.html.en">application integration</link> in GNOME 3 requires a desktop launcher. For this you need to create a <file>.desktop</file> file. The <file>.desktop</file> file describes the application name, the used icon and various integration bits. A deeper insight into the <file>.desktop</file> file can be found <link href="http://developer.gnome.org/desktop-entry-spec/">here</link>. The <file>.desktop.in</file> file will create the <file>.desktop</file>.</p>
<p>The example shows you the minimum requirements for a <code>.desktop.in</code> file.</p>
<code mime="text/desktop" style="numbered">[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;
</code>
<p>Now let's go through some parts of the <code>.desktop.in</code> file.</p>
<terms>
<item><title>Nom</title><p>The application name.</p></item>
<item><title>Comment</title><p>A short description of the application.</p></item>
<item><title>Exec</title><p>Specifies a command to execute when you choose the application from the menu. In this example exec just tells where to find the <file>hello-world</file> file and the file takes care of the rest.</p></item>
<item><title>Terminal</title><p>Specifies whether the command in the Exec key runs in a terminal window.</p></item>
</terms>
<p>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 <link href="http://standards.freedesktop.org/menu-spec/latest/apa.html">menu specification</link>.</p>
<p>In this example we use an existing icon. For a custom icon you need to have a .svg file of your icon, stored in <file>/usr/share/icons/hicolor/scalable/apps</file>. Write the name of your icon file to the .desktop.in file, on line 7. More information on icons in: <link href="https://live.gnome.org/GnomeGoals/AppIcon">Installing Icons for Themes</link> and <link href="http://freedesktop.org/wiki/Specifications/icon-theme-spec">on freedesktop.org: Specifications/icon-theme-spec</link>.</p>
</section>
<section id="autotools"><title>The build system</title>
<p>Pour que votre application fasse partie intégrante de GNOME 3, vous devez l'installer à l'aide d'autotools. Autotools construit et installe tous les fichiers nécessaires et les place à leurs emplacements corrects.</p>
<p>Auparavant, vous devez posséder les fichiers suivants :</p>
<links type="section"/>
<section id="autogen"><title>autogen.sh</title>
<code mime="application/x-shellscript" style="numbered">#!/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
</code>
<p>After the <file>autogen.sh</file> file is ready and saved, run:</p>
<screen><output style="prompt">$ </output><input>chmod +x autogen.sh</input></screen>
</section>
<section id="makefile"><title>Makefile.am</title>
<code mime="application/x-shellscript" style="numbered"># 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
</code>
</section>
<section id="configure"><title>configure.ac</title>
<code mime="application/x-shellscript" style="numbered"># 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
</code>
</section>
<section id="readme"><title>README</title>
<p>Information users should read first. This file can be blank.</p>
<p>When you have the <file>hello-world.c</file>, <file>hello-world.desktop.in</file>, <file>Makefile.am</file>, <file>configure.ac</file> and <file>autogen.sh</file> files with correct information and rights, the <file>README</file> file can include the following instructions:</p>
<code mime="text/readme" style="numbered">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
</code>
</section>
<!-- TODO: How to make a custom icon with autotools -->
</section>
</page>
|