Get MinGW Cross Compiler at SourceForge.net. Fast, secure and Free Open Source software downloads

Hello World program using GTK+ and Autotools


Note: gtk2-devel and related packages are required on your Linux system to use these samples mentioned in this page.

Additional files for Autotools

In order to build program using GNU Autotools, you need to add additional files for generating Makefile and configure script in the same directory as hello_gtk.c. The following configure.ac and Makefile.am have minimum descriptions just for building 'Hello World!' program from hello_gtk.c for both Win32 and Linux.

List : configure.ac

AC_PREREQ(2.57)

AC_INIT(hello, 1.0.0)

AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE

AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_INSTALL

GTK_REQUIRED_VERSION=2.0.0
GLIB_REQUIRED_VERSION=$GTK_REQUIRED_VERSION

AM_PATH_GLIB_2_0($GLIB_REQUIRED_VERSION,,
  [AC_MSG_ERROR(Test for GLib failed.)], gobject)
AM_PATH_GTK_2_0($GTK_REQUIRED_VERSION,,
  [AC_MSG_ERROR(Test for GTK failed.)])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

List : Makefile.am

bin_PROGRAMS = hello
hello_SOURCES = hello_gtk.c
hello_LDADD = $(GTK_LIBS)
hello_CFLAGS = $(GTK_CFLAGS)

Generating configure script

$ ls
Makefile.am  configure.ac  hello_gtk.c
$ touch AUTHORS ChangeLog NEWS README
$ aclocal; autoheader; automake -a -c; autoconf
configure.ac:9: installing `./compile'
configure.ac:6: installing `./install-sh'
configure.ac:6: installing `./missing'
Makefile.am: installing `./INSTALL'
Makefile.am: installing `./COPYING'
Makefile.am: installing `./depcomp'
$ ls
AUTHORS    INSTALL      NEWS        autom4te.cache  configure     hello_gtk.c
COPYING    Makefile.am  README      compile         configure.ac  install-sh
ChangeLog  Makefile.in  aclocal.m4  config.h.in     depcomp       missing

Building for MinGW

In order to do cross compile, you need to specify --host and --build options. The host system is MinGW, 'i686-pc-mingw32' and the build system is your Linux sistem, such as 'x86_64-redhat-linux' or 'i386-redhat-linux'.

Note: Latest mingw32-pkgconfig package is required in this build without setting correct value of PKG_CONFIG_PATH for MinGW Cross Compile environment. The package can be obtained at this download site.

Bulding hello.exe program for Win32

$ ./configure --build=x86_64-redhat-linux --host=i686-pc-mingw32 \
> --prefix=$HOME/tmp/hello
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 for i686-pc-mingw32-strip... i686-pc-mingw32-strip
checking for i686-pc-mingw32-gcc... i686-pc-mingw32-gcc
checking for C compiler default output file name... a.exe
checking whether the C compiler works... yes
checking whether we are cross compiling... yes
checking for suffix of executables... .exe
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether i686-pc-mingw32-gcc accepts -g... yes
checking for i686-pc-mingw32-gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of i686-pc-mingw32-gcc... gcc3
checking whether i686-pc-mingw32-gcc and cc understand -c and -o together... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking for i686-pc-mingw32-pkg-config... /usr/bin/i686-pc-mingw32-pkg-config
checking pkg-config is at least version 0.16... yes
checking for GLIB - version >= 2.0.0... cross compiling; assumed OK... 
yes (version 2.19.5)
checking for pkg-config... (cached) /usr/bin/i686-pc-mingw32-pkg-config
checking for GTK+ - version >= 2.0.0... cross compiling; assumed OK... 
yes (version 2.15.0)
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
$ make
make  all-am
make[1]: Entering directory `/home/bitwalk/mingw-cross/work/hello_gtk'.
i686-pc-mingw32-gcc -DHAVE_CONFIG_H -I.    -mms-bitfields -I/usr/i686-pc-mingw3
2/sys-root/mingw/include/gtk-2.0 -I/usr/i686-pc-mingw32/sys-root/mingw/lib/gtk-
2.0/include -I/usr/i686-pc-mingw32/sys-root/mingw/include/atk-1.0 -I/usr/i686-p
c-mingw32/sys-root/mingw/include/cairo -I/usr/i686-pc-mingw32/sys-root/mingw/in
clude/pango-1.0 -I/usr/i686-pc-mingw32/sys-root/mingw/include/glib-2.0 -I/usr/i
686-pc-mingw32/sys-root/mingw/lib/glib-2.0/include -I/usr/i686-pc-mingw32/sys-r
oot/mingw/include/freetype2 -I/usr/i686-pc-mingw32/sys-root/mingw/include   -g 
-O2 -MT hello-hello_gtk.o -MD -MP -MF .deps/hello-hello_gtk.Tpo -c -o hello-hel
lo_gtk.o `test -f 'hello_gtk.c' || echo './'`hello_gtk.c
mv -f .deps/hello-hello_gtk.Tpo .deps/hello-hello_gtk.Po
i686-pc-mingw32-gcc -mms-bitfields -I/usr/i686-pc-mingw32/sys-root/mingw/includ
e/gtk-2.0 -I/usr/i686-pc-mingw32/sys-root/mingw/lib/gtk-2.0/include -I/usr/i686
-pc-mingw32/sys-root/mingw/include/atk-1.0 -I/usr/i686-pc-mingw32/sys-root/ming
w/include/cairo -I/usr/i686-pc-mingw32/sys-root/mingw/include/pango-1.0 -I/usr/
i686-pc-mingw32/sys-root/mingw/include/glib-2.0 -I/usr/i686-pc-mingw32/sys-root
/mingw/lib/glib-2.0/include -I/usr/i686-pc-mingw32/sys-root/mingw/include/freet
ype2 -I/usr/i686-pc-mingw32/sys-root/mingw/include   -g -O2   -o hello.exe hell
o-hello_gtk.o -Wl,-luuid -L/usr/i686-pc-mingw32/sys-root/mingw/lib -lgtk-win32-
2.0 -lgdk-win32-2.0 -limm32 -lshell32 -lole32 -latk-1.0 -lgdk_pixbuf-2.0 -lpang
ocairo-1.0 -lgio-2.0 -lcairo -lpangoft2-1.0 -lpangowin32-1.0 -lgdi32 -lfreetype
 -lz -lfontconfig -lpango-1.0 -lm -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl   
make[1]: Leaving directory `/home/bitwalk/mingw-cross/work/hello_gtk'.
$ make install
make[1]: Entering directory `/home/bitwalk/mingw-cross/work/hello_gtk'.
test -z "/home/bitwalk/tmp/hello/bin" || /bin/mkdir -p "/home/bitwalk/tmp/hello
/bin"
  /usr/bin/install -c 'hello.exe' '/home/bitwalk/tmp/hello/bin/hello.exe'
make[1]: Nothing to be done for `install-data-am'.
make[1]: Leaving Directory `/home/bitwalk/mingw-cross/work/hello_gtk'.
$ ./hello.exe &
 

figure : hello.exe program executed on Wine

Building for Linux

If you want to build this program for native Linux, you can build it as shown below without any changes on sources.

Bulding hello program for Linux

$ make clean
test -z "hello.exe" || rm -f hello.exe
rm -f *.o
$ ./configure --prefix=$HOME/tmp/hello_linux
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 for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
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 for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking whether gcc and cc understand -c and -o together... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.16... yes
checking for GLIB - version >= 2.0.0... yes (version 2.18.3)
checking for pkg-config... /usr/bin/pkg-config
checking for GTK+ - version >= 2.0.0... yes (version 2.14.5)
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
$ make
make  all-am
make[1]: Entering directory `/home/bitwalk/mingw-cross/work/hello_gtk'
gcc -DHAVE_CONFIG_H -I.    -I/usr/include/gtk-2.0 -I/usr/lib64/gtk-2.0/include 
-I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/inc
lude/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/freetype2   -g -O2 -
MT hello-hello_gtk.o -MD -MP -MF .deps/hello-hello_gtk.Tpo -c -o hello-hello_gt
k.o `test -f 'hello_gtk.c' || echo './'`hello_gtk.c
mv -f .deps/hello-hello_gtk.Tpo .deps/hello-hello_gtk.Po
gcc -I/usr/include/gtk-2.0 -I/usr/lib64/gtk-2.0/include -I/usr/include/atk-1.0 
-I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/li
b64/glib-2.0/include -I/usr/include/freetype2   -g -O2   -o hello hello-hello_g
tk.o -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lgdk_pixbu
f-2.0 -lpangocairo-1.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.
0 -lgmodule-2.0 -lglib-2.0   
make[1]: Leaving directory `/home/bitwalk/mingw-cross/work/hello_gtk'
$ make install
make[1]: Entering directory `/home/bitwalk/mingw-cross/work/hello_gtk'
test -z "/home/bitwalk/tmp/hello_linux/bin" || /bin/mkdir -p "/home/bitwalk/tmp
/hello_linux/bin"
  /usr/bin/install -c 'hello' '/home/bitwalk/tmp/hello_linux/bin/hello'
make[1]: Nothing to be done for `install-data-am'.
make[1]: Leaving directory `/home/bitwalk/mingw-cross/work/hello_gtk'
$ ./hello &
 

figure : hello program executed on Linux

Related Links

  1. GNU Autoconf, Automake and Libtool
  2. Autoconf - GNU Project - Free Software Foundation (FSF)
  3. Automake - GNU Project - Free Software Foundation (FSF)
  4. Welcome to Autotoolset Home Page!