GTK3 - C programming - ch1

Home

Table of Contents

1 Introduction

what is this section about?

2 Reference material

3 Example0

3.1 Makefile

3.2 example-0.c

#include <gtk/gtk.h>

int
main (int   argc,
      char *argv[])
{
  GtkWidget *window;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Window");

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}
example-0:
        gcc `pkg-config --cflags gtk+-3.0` -o example-0 example-0.c `pkg-config --libs gtk+-3.0`

compile with make:

make

4 Example0 with autotools

Same C source file, but compiled with GNU autotools.

Using autotools in GTK3 projects: PKG_CHECK_MODULES macro https://autotools.io/pkgconfig/pkg_check_modules.html The main interface between autoconf and pkg-config is the PKG_CHECK_MODULES macro, which provides a very basic and easy way to check for the presence of a given package in the system.

files in this example:

  • configure.ac
  • Makefile.am
  • src/Makefile.am
  • src/example-0.c

4.1 configure.ac

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([example0], [0.1], [snarvaezsoft at gmail dot com])
AM_INIT_AUTOMAKE([-Wall -Werror])
AC_CONFIG_SRCDIR([src/example-0.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# https://autotools.io/pkgconfig/pkg_check_modules.html
# The main interface between autoconf and pkg-config is the PKG_CHECK_MODULES macro, which provides a very basic and easy way to check for the presence of a given package in the system
# By default, the macro will set up two variables, joining the given prefix with the suffixes _CFLAGS and _LIBS.
# As we are using the GTK3 prefix, This will setup the variable GTK3_CFLAGS  with the output of  pkg-config --cflags gtk+-3.0
# AND GTK3_LIBS with the output of: pkg-config --libs gtk+-3.0
# Those variables are used later in the file  src/Makefile.am
PKG_CHECK_MODULES([GTK3], [gtk+-3.0])


# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.


AC_CONFIG_FILES([Makefile
                 src/Makefile])

AC_OUTPUT

4.2 Makefile.am

## Makefile.am -- Process this file with automake to produce Makefile.in

SUBDIRS = src

## Define an independent executable script for inclusion in the distribution
## archive. It will not be installed on an end user's system, however.
dist_noinst_SCRIPTS = autogen.sh

4.3 src/Makefile.am

## Makefile.am -- Process this file with automake to produce Makefile.in

## run with:
## make clean
## make CXXFLAGS='-g -Wall -O0 '  CFLAGS='-g -Wall -O0 '

## Append to CFLAGS.  Used by gcc to compile c programs
AM_CFLAGS =  -std=gnu11

## Append to CXXFLAGS. Used by g++ to complite cpp programs
AM_CXXFLAGS = -std=c++11

## Place generated object files (.o) into the same directory as their source
## files, in order to avoid collisions when non-recursive make is used.
AUTOMAKE_OPTIONS = subdir-objects

## Additional flags to pass to aclocal when it is invoked automatically at
## make time. The ${ACLOCAL_FLAGS} variable is picked up from the environment
## to provide a way for the user to supply additional arguments.
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}

## Set the default command-line flags for the C preprocessor to the value
## obtained from pkg-config via PKG_CHECK_MODULES in configure.ac.  These
## flags are passed to the compiler for both C and C++, in addition to the
## language-specific options.
AM_CPPFLAGS = $(GTK3_CFLAGS)

## Define an executable target, which will be installed into the
## directory named by the predefined variable $(bindir).
bin_PROGRAMS = example-0

## Set the library dependencies for the "example" target to the value obtained
## from pkg-config via PKG_CHECK_MODULES in configure.ac.  These libraries are
## passed to the linker in addition to the other linker flags.
example_0_LDADD = $(GTK3_LIBS)

4.4 src/example-0.c

#include <gtk/gtk.h>

int
main (int   argc,
      char *argv[])
{
  GtkWidget *window;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Window");

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}

4.5 generating configure and Makefile

First run autoscan to generate the file configure.scan with a template for a configure.ac file. You can inspect the contents of configure.scan and compare with your configure.ac or take it as base for configure.ac

autoscan

Autoreconf will try to take care of running the tools in the correct order…

autoreconf

configure.ac:11: error: required file './compile' not found
configure.ac:11:   'automake --add-missing' can install 'compile'
configure.ac:6: error: required file './install-sh' not found
configure.ac:6:   'automake --add-missing' can install 'install-sh'
configure.ac:6: error: required file './missing' not found
configure.ac:6:   'automake --add-missing' can install 'missing'
Makefile.am: error: required file './INSTALL' not found
Makefile.am:   'automake --add-missing' can install 'INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: error: required file './COPYING' not found
Makefile.am:   'automake --add-missing' can install 'COPYING'
src/Makefile.am: error: required file './depcomp' not found
src/Makefile.am:   'automake --add-missing' can install 'depcomp'
autoreconf: automake failed with exit status: 1
automake --add-missing

configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
src/Makefile.am: installing './depcomp'

Adding the missing files that automake complains about …

touch NEWS README AUTHORS ChangeLog

This time autoreconf runs silently and generates the file configure

autoreconf

Now running configure will generate the Makefiles

./configure

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for GTK3... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands

Running make to compile the project

make

make  all-recursive
make[1]: Entering directory '/home/seba/c_workspace/sen-blog/public_html/gtk3/examples/example0autotools'
Making all in src
make[2]: Entering directory '/home/seba/c_workspace/sen-blog/public_html/gtk3/examples/example0autotools/src'
depbase=`echo example-0.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
gcc -DHAVE_CONFIG_H -I. -I..  -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include   -std=gnu11 -g -O2 -MT example-0.o -MD -MP -MF $depbase.Tpo -c -o example-0.o example-0.c &&\
mv -f $depbase.Tpo $depbase.Po
gcc -std=gnu11 -g -O2   -o example-0 example-0.o -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

The result should be an executable file example-0 in the src directory.

5 Required packages to compile the examples

tested on Debian 8

5.1 Core libraries

libglib2.0-dev 9 (GIO and GObject are already inside this package)

libglib2.0-doc

5.2 User interface libraries

libgtk-3-dev

libgtk-3-doc

libcairo2-dev

libcairo2-doc

libpango1.0-dev

libpango1.0-doc

libatk1.0-dev

libatk1.0-doc

libclutter-1.0-dev

libclutter-1.0-doc

libwebkitgtk-3.0-dev

libwebkitgtk-3.0-doc

5.3 Multimedia libraries

libgstreamer0.10-dev

5.4 Data storage

libecal1.2-dev

5.5 Tools and basic development packages

valac-0.16

anjuta

glade

gtranslator

devhelp

6 Detailed packages info

ii  anjuta                                2:3.14.0-1                        amd64        GNOME development IDE, for C/C++
ii  anjuta-common                         2:3.14.0-1                        all          GNOME development IDE, for C/C++ - data files

ii  autoconf                              2.69-8                            all          automatic configure script builder
ii  autogen                               1:5.18.4-3                        amd64        automated text file generator
ii  autogen-doc                           1:5.18.4-3                        all          automated text file generator - documentation
ii  automake                              1:1.14.1-4                        all          Tool for generating GNU Standards-compliant Makefiles

ii  autotools-dev                         20140911.1                        all          Update infrastructure for config.{guess,sub} files

ii  binutils-doc                          2.25-5                            all          Documentation for the GNU assembler, linker and binary utilities

ii  build-essential                       11.7                              amd64        Informational list of build-essential packages



ii  libgtk-3-0:amd64                      3.14.5-1                          amd64        GTK+ graphical user interface library
ii  libgtk-3-bin                          3.14.5-1                          amd64        programs for the GTK+ graphical user interface library
ii  libgtk-3-common                       3.14.5-1                          all          common files for the GTK+ graphical user interface library
ii  libgtk-3-dev:amd64                    3.14.5-1                          amd64        development files for the GTK+ library
ii  libgtk-3-doc                          3.14.5-1                          all          documentation for the GTK+ graphical user interface library



ii  cpp                                   4:4.9.2-2                         amd64        GNU C preprocessor (cpp)
ii  cpp-4.8                               4.8.4-1                           amd64        GNU C preprocessor
ii  cpp-4.9                               4.9.2-10                          amd64        GNU C preprocessor

ii  devhelp                               3.14.0-1                          amd64        GNOME developers help program
ii  devhelp-common                        3.14.0-1                          all          Common files for devhelp and its library

ii  g++                                   4:4.9.2-2                         amd64        GNU C++ compiler
ii  g++-4.9                               4.9.2-10                          amd64        GNU C++ compiler


ii  gcc                                   4:4.9.2-2                         amd64        GNU C compiler
ii  gcc-4.8                               4.8.4-1                           amd64        GNU C compiler
ii  gcc-4.8-base:amd64                    4.8.4-1                           amd64        GCC, the GNU Compiler Collection (base package)
ii  gcc-4.9                               4.9.2-10                          amd64        GNU C compiler
ii  gcc-4.9-base:amd64                    4.9.2-10                          amd64        GCC, the GNU Compiler Collection (base package)

ii  gdb                                   7.7.1+dfsg-5                      amd64        GNU Debugger
ii  gdb-doc                               7.7.1+dfsg-2                      all          The GNU Debugger Documentation
ii  gdbserver                             7.7.1+dfsg-5                      amd64        GNU Debugger (remote server)


ii  git                                   1:2.1.4-2.1                       amd64        fast, scalable, distributed revision control system
ii  git-man                               1:2.1.4-2.1                       all          fast, scalable, distributed revision control system (manual pages)


ii  glade                                 3.18.3-1                          amd64        GTK+ User Interface Builder

ii  glibc-doc-reference                   2.19-1                            all          GNU C Library: Documentation


ii  libc-bin                              2.19-15                           amd64        GNU C Library: Binaries
ii  libc-dev-bin                          2.19-15                           amd64        GNU C Library: Development binaries
ii  libc6:amd64                           2.19-15                           amd64        GNU C Library: Shared libraries
ii  libc6-dbg:amd64                       2.19-15                           amd64        GNU C Library: detached debugging symbols
ii  libc6-dev:amd64                       2.19-15                           amd64        GNU C Library: Development Libraries and Header Files

ii  libcairo2:amd64                       1.14.0-2.1                        amd64        Cairo 2D vector graphics library
ii  libcairo2-dev                         1.14.0-2.1                        amd64        Development files for the Cairo 2D graphics library
ii  libcairo2-doc                         1.14.0-2.1                        all          Documentation for the Cairo Multi-platform 2D graphics library

ii  libcanberra-gtk-module:amd64          0.30-2.1                          amd64        translates GTK+ widgets signals to event sounds
ii  libcanberra-gtk0:amd64                0.30-2.1                          amd64        GTK+ helper for playing widget event sounds with libcanberra
ii  libcanberra-gtk3-0:amd64              0.30-2.1                          amd64        GTK+ 3.0 helper for playing widget event sounds with libcanberra
ii  libcanberra-gtk3-module:amd64         0.30-2.1                          amd64        translates GTK3 widgets signals to event sounds

ii  libclang-common-3.5-dev               1:3.5-10                          amd64        clang library - Common development package
ii  libclang1-3.5:amd64                   1:3.5-10                          amd64        C interface to the clang library


ii  libglib2.0-0:amd64                    2.42.1-1                          amd64        GLib library of C routines
ii  libglib2.0-bin                        2.42.1-1                          amd64        Programs for the GLib library
ii  libglib2.0-data                       2.42.1-1                          all          Common files for GLib library
ii  libglib2.0-dev                        2.42.1-1                          amd64        Development files for the GLib library
ii  libglib2.0-doc                        2.42.1-1                          all          Documentation files for the GLib library
ii  libglibmm-2.4-1c2a:amd64              2.42.0-1                          amd64        C++ wrapper for the GLib toolkit (shared libraries)


ii  libstdc++6:amd64                      4.9.2-10                          amd64        GNU Standard C++ Library v3



ii  m4                                    1.4.17-4                          amd64        macro processing language

ii  zlib1g:amd64                          1:1.2.8.dfsg-2+b1                 amd64        compression library - runtime
ii  zlib1g-dev:amd64                      1:1.2.8.dfsg-2+b1                 amd64        compression library - development

==================================================

EXTRAS


ii  ack-grep                              2.14-4                            all          grep-like program specifically for large source trees


ii  llvm-3.5                              1:3.5-10                          amd64        Modular compiler and toolchain technologies
ii  llvm-3.5-dev                          1:3.5-10                          amd64        Modular compiler and toolchain technologies, libraries and headers
ii  llvm-3.5-runtime                      1:3.5-10                          amd64        Modular compiler and toolchain technologies, IR interpreter

ii  clang                                 1:3.5-25                          amd64        C, C++ and Objective-C compiler (LLVM based)
ii  clang-3.5                             1:3.5-10                          amd64        C, C++ and Objective-C compiler (LLVM based)


ii  lua-dbi-common                        0.5.hg5ba1dd988961-2              all          DBI library for the Lua language, common files
ii  lua-dbi-mysql:amd64                   0.5.hg5ba1dd988961-2              amd64        DBI library for the Lua language, MySQL backend
ii  lua-mode                              20140514-2                        all          Emacs mode for editing Lua programs
ii  lua-unit                              3.0-1                             all          Lua unit testing framework
ii  lua5.2                                5.2.3-1.1                         amd64        Simple, extensible, embeddable programming language
ii  lua5.2-doc                            5.2.3-1.1                         all          Documentation for the Lua language version 5.2

7 Compiling the examples

Compile with the standard ./configure ; make

Additionally add the following make options to turn off optimizations and enable debugging symbols

./configure

make CXXFLAGS='-g3 -Wall -O0 '  CFLAGS='-g3 -Wall -O0 '

8 Autotools template example

Please check the Autotools template example

Author: Sebastian Emilio Narvaez

Created: 2019-10-12 Sat 22:16

Emacs 25.2.2 (Org mode 8.2.10)

Validate