Monday, March 26, 2007

GTK+ UNIX To Win32 Porting Tutorial

By WolfPack Entertainment

2002 May 5

This is a tutorial on how to port a UNIX/GTK+ application to Win32, it is based on the information used by WolfPack Entertainment to port their own UNIX/GTK+ applications to Win32.

You will need the following in order to port your UNIX/GTK+ application to Win32 using this tutorial:

  • The source for the application that you want to port
  • VC++ installed and working properly on a Windows machine
Make sure that the GTK+ application that you are porting can be compiled successfully and that it runs properly on a UNIX machine with GTK+ 2.0.

Contents

  1. Installing GTK+ Win32 Devel on your Windows machine
  2. Set up VC++ to compile with GTK+ Win32 Devel
  3. GDKX and GDKWin32 Devel Set Up
  4. Project Set Up & Compiling
  5. Installing GTK+ Win32 DLLs on your Windows machine
  6. Common Errors & Solutions

Installing GTK+ Win32 Devel on your Windows machine

These files only need to be installed on the Windows machine that you are going to compile the applications on (not to run applications), your end users will not need to install these.

The files that you need to download are listed at:

The exact GTK+ Win32 Devel and GTK+ Win32 DLL package files that you need to download are as follows: Note that you are getting both Devel and DLLs packages in one step here, later you will sort out exactly what your end users will need.

Create a subdirectory in which you want to place your GTK+ Win32 Devel files in, on our Windows machine we used:

  • D:\GTKWIN32\
And subsequently have the typical devel subdirectories in there, such as:
  • D:\GTKWIN32\BIN\
  • D:\GTKWIN32\DOC\
  • D:\GTKWIN32\ETC\
  • D:\GTKWIN32\INCLUDE\
  • D:\GTKWIN32\LIB\
  • D:\GTKWIN32\MAN\
  • D:\GTKWIN32\SHARE\
For this tutorial we will reffer to the paths using the above situation.

Move all the package files that you have just downloaded in to:

  • D:\GTKWIN32\
And then unzip all of them (preserving subdirectories).

Note: The file COPYING.LIB-2 is common in most packages, you will be prompted to overwrite it when you extract (respond yes to that). Also, there is a duplicate file named lib\pkgconfig\libpng.pc found in both packages libpng-1.2.0.zip and zlib-1.1.3.zip (we used the one from zlib-1.1.3.zip since it was slightly newer).

[ Top of Section | Contents ]


Set up VC++ to compile with GTK+ Win32 Devel

You should now run VC++'s IDE and go to Tools->Options... Click on the Directories tab. There you need to define the directories for the GTK+ Win32 Devel files. Below are the directories that we defined:

One developer noted that we should place these directories before any MSSDK to avoid conflict (however we never experienced any problem and cannot confirm this).

[ Top of Section | Contents ]


GDKX and GDKWin32 Devel Set Up

Most GTK+ applications need to access some X resources, since Win32 has no X for obvious reasons, there is something called GDKWin32. The short story is that if you #include then you need to change that to #include . You can make this a compile time condition, for example:


#ifdef WIN32
# include
#else
# include
#endif

Important note: The packages that you downloaded in the above appear to be missing a particular GDKWin32 header file. You need to obtain that file here:

And install it in D:\GTKWIN32\INCLUDE\GDK\WIN32\GDKPRIVATE-WIN32.H

[ Top of Section | Contents ]


Project Set Up & Compiling

Now you need to create a project (VC++'s equvilent to Makefiles) for your GTK+ application. Make sure that you have the complete UNIX version of your GTK+ application's source on your Windows machine. Create a new project in the source directory of your GTK+ application (File->New) and select New Win32 Application.

Now you have a Workspace/Project, next you need to add your source files to the project and also make sure that you add the following .lib files:

Look for those in D:\GTKWIN32\LIB\

At this point you are ready to try and compile. There may be errors and there probably will be errors, if you do encounter errors please read the section Common Errors and Solutions

If you do manage to compile things successfully, do not run your program just yet. You will need to install the GTK+ Win32 DLLs next.


Installing GTK+ Win32 DLLs on your Windows machine

The GTK+ Win32 DLL need to be installed in your C:\WINDOWS\SYSTEM\ directory, most GTK+ applications ported to Win32 seem to require the following DLLs in your C:\WINDOWS\SYSTEM\ directory:

You can find these files originally in the D:\GTKWIN32\LIB\ directory when you initially extracted, move them to C:\WINDOWS\SYSTEM\

Your GTK+ program may require more or less, however the easiest way to find out is to simply run your GTK+ application now (provided that was compiled without any errors or serious warnings).

[ Top of Section | Contents ]


Common Errors & Solutions

Error: Warnings about libc conflict at linking stage.
Possible Solution: In Settings->Link->Input you should add to the Project Options /nodefaultlib:"libc" (or you can just add libc to the Ignore Libraries: line)

Error: Segfaults at runtime on free() calls.
Possible Solution: If this behavior was not observed on the UNIX version of your application then the most likely cause is that you allocated memory with g_malloc() or similar function and deallocated with free() instead of g_free(). You need to use g_free() and you cannot mix libc (de)allocators with glib (de)allocators.

Error: Console appears and I see a bunch of GDK gdl_draw*() errors printed.
Possible Solution: If this behavior was not observed on the UNIX version of your application then the most likely cause is that the gdk_draw_*() function received values that were out of bounds or 0 for a asserted non-zero input. The Win32 GDK appears to need more bounds checking before input to its gdk_draw_*() functions.

[ Top of Section | Contents ]

No comments: