You are here

Add new comment

Tutorial : cross-compile a C/GTK+3 program from Linux to Windows

Objectives : compile a C/GTK+3 application under Linux, and produce a valid Windows© binary.

We have a C application using GTK+ 3 as its graphical toolkit :

As the GTK+ 3 Windows binaries now have an official distribution, we will be able to use to create a Windows version of our application, without modifiying the source code nor having a Windows box available.

Prerequisites

A Linux Debian or Ubuntu distribution.

1) Install the compilation toolchain

- Install the GCC compiler for Windows (namely MinGW) :

apt-get install mingw32

- Download the latest all-in-one bundle ZIP archive directly from the official website (here's a direct link for impatients...).

(ps : Create a folder named "gtk3-win32" where you want, put the ZIP archive therein and extract it with :
unzip gtk+-bundle_3.6.4-20130921_win32.zip)


- Adapt GTK+ to its location :
In a terminal, move to the "gtk3-win32" folder you just created. If you extracted it to "/opt/gtk3-win32" e.g. :
cd /opt/gtk3-win32
then do :
find -name '*.pc' | while read pc; do sed -e "s@^prefix=.*@prefix=$PWD@" -i "$pc"; done

2) Compile

- We will tell "pkg-config" to locate GTK+ libraries in our custom path. If you extracted to "/opt/gtk3-win32" e.g. :
export PKG_CONFIG_PATH=/opt/gtk3-win32/lib/pkgconfig

We are ready to compile an sample C source ! Let's use a command in this style :

i586-mingw32msvc-gcc source.c -o executable.exe `pkg-config --cflags --libs gtk+-3.0`

A new executable should have been created if everything went well.
It won't run on our Linux system, because it's targeting Windows !

3) Release

- Create a folder containing the binary and the Windows .dll files. If you extracted to "/opt/gtk3-win32" e.g. :
mkdir ~/distri
cp executable.exe ~/distri
cp /opt/gtk3-win32/bin/*.dll ~/distri

- Here we go ! Transfer this folder to a Windows box. Double-click on the executable and...

Well done !

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.