Make your own free website on Tripod.com

Amateur HowTo of GTK+ for Windows

Pandhare S.M.

March 2004


Table of Contents


Introduction

This HowTo is inspired from "mini-Tutorial: How to install GTK+ on Dev-C++ 5" by Kylek posted at forum on http://www.dropline.net. There is also good tutorial at http://www.dropline.net/gtk/support.php. But Still I found little difficult to start. Finally replies from Tor Lillqvist <tml iki fi> to Suresh Kumar on subject "GTK+ and MinGw.............." at
http://mail.gnome.org/archives/gtk-app-devel-list/2003-September/thread.html#00149 came to rescue me. And I found that actual procedure is a little more simple than first two mentioned tutorial. The other drawback is that these tutorials does not provides much insights. And they do not discuss MinGW. However I should admit that these two tutorials also helped me lot. And I recommend these links since my tutorial contains excerpts from them.

Why amateur HowTo? Because I am a newbie of all things like GUI programming, GTK+, gcc, Linux, dos. Actually I am spoon-fed window user with some C programming knowledge under my belt. Recently I have come across Open-source and I want to persuade my friend to use GTK+. This howto comes out of help to him. Hence, though I boasts of simplest procedure (but may not shortest to read), do not assume that it works for all situations or best practice of using GTK+. And always doubt my knowledge or insights.

Note: All in Bold Italic are comments from me or modifications by me.

System Setup  

The procedure, I describe, found working with Version 2.2.4.1 of both Runtime Environment and Development Environment on Windows 98 and Windows XP. The compilers I have used are MinGW-2.0.0-3 (which comes with gcc version 3.2 (mingw special 20020817-1)) and Bloodshed Dec-C++ 5 beta 2 release 4.9.2.0 (which comes with gcc version 2.95.3-6 (mingw special)).See section Finding version of gcc in DOS. The other software versions mentioned are given on download link or mentioned in the name of set-up program or comes up while installing or comes up on title-bar when you run installed program or on clicking About in Help menu.

Download links

Version 2.2.4.1 of both Runtime and Development Environment of GTK+: http://www.dropline.net/gtk/download.php

Bloodshed Dev-C++: http://www.bloodshed.net/dev/devcpp.html . Download latest version than I mentioned.

MinGW:  http://www.mingw.org . I do not know exact link. I got on magazine CD. Download latest version than I mentioned.

You can also visit the site http://gtk.org or http://gtk.org/win32. But I have downloaded nothing from it.

Installation

I have chosen installation paths considering 8-character limitation of directory of file name for the DOS. I have chosen 'C:\' drive but you can install to any drive. (Following are the paths which should be given during installation. They are often called as 'Base Directories'.)

GTK+ Development Environment: C:\GTK\develop\
GTK+ Runtime Environment: C:\GTK\runtime\

Bloodshed Dev-C++: C:\Dev-C++\
MinGW Compiler: C:\MinGW\

It is sufficient to install either MinGW or Dev-C++.

Finding version of gcc in DOS

The file gcc.exe is located in 'bin' folder of base directory of compiler system. If MinGW is installed to C:\Mingwthen run

cd C:\MinGW\bin
gcc -v

If Bloodshed Dev-C++ is installed at C:\Dev-C++ then run

cd C:\Dev-C++\bin
gcc -v

You can give direct command gcc -v if C:\MinGW\bin or C:\Dev-C++\bin is in PATH variable discussed in section DOS basics.

GTK+ in Linux

You will get some more insights if you consider how GTK+ works on Linux.

Copy the helloworld program from 'Getting Started' page at http://gtk.org/tutorial/ and paste in suitable editor and save as helloworld.c to suitable folder. Then on command line (terminal), change the working directory to the folder in which helloworld.c lies. e.g. $cd \home\my_name\tutor. And run the below statement in above mentioned tutorial.

gcc -Wall -g helloworld.c -o helloworld `pkg-config --cflags gtk+-2.0` \
`pkg-config --libs gtk+-2.0`
And to run the file, type

./helloworld

(excerpt from above mentioned tutorial)
pkg-config --cflags gtk+-2.0 will output a list of include directories for the compiler to look in, and pkg-config --libs gtk+-2.0 will output the list of libraries for the compiler to link with and the directories to find them in. In the above example they could have been combined into a single instance, such as pkg-config --cflags --libs gtk+-2.0. Note that the type of single quote used in the compile command above is significant. (It is backquote. It is just above 'Tab' key on keyboard.)

As Tor Lillqvist writes in reply to Suresh Kumar ( See mailing list mentioned above), pkg-config isn't supposed to be an argument to gcc. Do you see those backquotes (the ` "quotes" or "accents") in the command line? In Unix-style shells, they mean that the stuff between backquotes is run as a command by itself, and the *output* of that command is then pasted in instead. Please note that the Windows Command Prompt (cmd.exe or command.com) doesn't understand the Unix shell style
backquote syntax (the ` stuff). (So you have to run pkg-config with appropriate options. Get the output and append it to gcc -Wall -g helloworld.c -o helloworld.) But instead of typing that by hand every time, at least put the command(s) in a .bat file. Preferably, learn to use Make. (I do not know about later.)

DOS Basics

You can skip this section if you know about how to set PATH, how to use batch files and how to redirect output to a file.

(excerpts from Windows help)

Path

Sets the command path in the PATH environment variable, which is the set of directories used to search for executable files.

Syntax
path [[%path%] [Drive:]Path [;...]]

Parameters

[Drive:]Path
Specifies the drive and directory to set in the command path.

;
Separates directories in the command path.

%path%
Replaces %path% with the existing set of directories listed in the PATH environment variable.

Using batch files   

A batch file is an unformatted text file that contains one or more commands and has a .bat or .cmd file name extension. When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file.

Using batch parameters   

MS-DOS provides the batch parameter expansion variables %0 through %9. When you use batch parameters in a batch file, %0 is replaced by the batch file name, and %1 through %9 are replaced by the corresponding arguments that you type at the command line.

For example, to copy the contents from Folder1 to Folder2, where %1 is replaced by the value Folder1 and %2 is replaced by the value Folder2, type the following in a batch file called mybatch.bat:

xcopy %1\*.* %2

To run the file, type:

mybatch.bat C:\folder1 D:\folder2

This has the same effect as typing the following in the batch file:

xcopy C:\folder1 \*.* D:\folder2

Redirecting command output (>)

Almost all commands send output to your Command Prompt window. To redirect command output from the Command Prompt window to a file or device, use the > operator. For example, to redirect dir output todirlist.txt:

dir>dirlist.txt

Use of pkg-config 

pkg-config utility is used for same purpose as in Linux. But DOS has not facility of backquotes as in Linux (to best of my knowledge). You have to use it in different way. Create a folder called 'tutor' on C: drive. We are going to store all our files in this folder. Then type following commands in DOS.

path C:\GTK\develop\bin;C:\GTK\runtime\lib;%path%
C:
cd tutor
pkg-config --cflags gtk+-2.0 >header.txt
pkg-config --libs gtk+-2.0 >linker.txt

You will get the files header.txt and linker.txt in folder C:\tutor. Thecontents of header.txt on machine I work are as follows:
-IC:/GTK/develop/include/gtk-2.0 -IC:/GTK/develop/lib/gtk-2.0/include -IC:/GTK/develop/include/atk-1.0 -IC:/GTK/develop/include/pango-1.0 -IC:/GTK/develop/include/glib-2.0 -IC:/GTK/develop/lib/glib-2.0/include 

pkg-config --cflags gtk+-2.0 will output a list of include directories for the compiler to look in.

(excerpt from Directory Options: GNU C Compiler User's Guide For GCC Version 2.8.1)

-Idir

Add the directory dir to the head of the list of directories to be searched for header files. If you use more than one `-I' option, the directories are scanned in left-to-right order; the standard system directories come after.

Do not worry about '/' sign instead of '\' in above directory paths.

and the contents of linker.txt are
-LC:/GTK/develop/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -liconv 

pkg-config --libs gtk+-2.0 will output the list of libraries for the compiler to link with and the directories to find them in.

(excerpt from Linker Options: GNU C Compiler User's Guide For GCC Version 2.8.1)

-llibrary

Search the library named library when linking. The linker searches processes libraries and object files in the order they are specified. The linker searches a standard list of directories for the library, which is actually a file named `liblibrary.a'.  The directories searched include several standard system directories plus any that you specify with `-L'.

Creation of  .bat file for using GTK+

I used contents of header.txt and linker.txt and typed following contents in my favorite editor and saved file as C:\tutor\gtkdev.bat.
path C:\MinGW\bin;C:\GTK\runtime\lib;%path%

gcc -Wall -g %1 -o %2 -mno-cygwin -mms-bitfields
-IC:/GTK/develop/include/gtk-2.0 -IC:/GTK/develop/lib/gtk-2.0/include -IC:/GTK/develop/include/atk-1.0 -IC:/GTK/develop/include/pango-1.0 -IC:/GTK/develop/include/glib-2.0 -IC:/GTK/develop/lib/glib-2.0/include -LC:/GTK/develop/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -liconv

%2

%1 %2

These are batch parameters. %1 is refers to C source file e.g. hello.c while %2 refers to name of executable file (to be created) with extension. If current directory is C:\tutor then use of above .bat file can be 'gtkdev.bat hello.c hello.exe' , where hello.c is C source file to be compiled and hello.exe is executable file to be created.


-mno-cygwin -mms-bitfields

According to Kylek  in his mini tutorial, this will make sure it doesn't find incorrectly sized windows and such.

According to http://www.dropline.net/gtk/support.php,
If you're using gcc-2.95.x to compile, you'll need to pass the -mno-cygwin and -fnative-struct options to the compiler. With gcc-3.x, replace -fnative-struct with -mms-bitfields.

I am sorry because I can not give insights about this options. But it seems that it is necessary to guarantee correct operations. If you know something  more then following excerpt from 'Using GTK+ in your own programs' athttp://www.gimp.org/~tml/gimp/win32/ may give clarification:

With gcc, you should use the -mno-cygwin and -mms-bitfields flags. ..... (Hint: struct field alignment. Hint 2: msvcrt.dll.) .......

C:\MinGW\bin

This is path of gcc.exe. If you want to use (in DOS) compiler provided with Bloodshed Dev-C++ , then type C:\Dev-C++\bin instead of C:\MinGW\bin. The Bloodshed Dev-C++ 4.9.2.0 comes with gcc version 2.95.3-6 (mingw special)which will require use of  -fnative-struct instead of  -mms-bitfields.

C:\GTK\runtime\lib

This is path of DLLs which provide GTK+ runtime environment. This is required to execute last line of  above .bat file i.e. %2 i.e. hello.exe if it is provided as batch parameter. Because whatever executable file created by gcc is required to be dynamically linked with the DLLs lying in folder C:\GTK\runtime\lib.

Relax! Here ends the part of howto which you should do only once (not repeatedly).

Compiling in DOS

Go to  http://gtk.org/tutorial/. Copy the helloworld program from 'Getting Started' page and paste in suitable editor and save as C:\tutor\hello.c. Then on command line (terminal), change the working directory to where gtkdev.bat lies.

C:
cd tutor

and then run

gtkdev.bat hello.c hello.exe

After execution of  command, a new window will open. Click on "Hello World" on it and window will close printing "Hello World" to DOS screen. (If you have not installed 'MS Sans Serif 8' font, you will get warning message with indication of use of another font 'sans 8'. Program will execute normally except the warning message. I found this problem on Windows 98.)

If gtkdev.bat lies in C:\tutor\, helllo.c lies in C:\temp1 and you want to create hello.exe in C:\temp2, then run following commands:

C:
cd tutor
gtkdev.bat C:\temp1\hello.c C:\temp2\hello.exe

If you want to develop a GTK+ program on Windows in an IDE, then a good choice will be Bloodshed Dev-C++. Let us see How to compile program using Dev-C++.

Compiling Using Bloodshed Dev-C++ 

On File menu, select  New and click Project. In File, type C:\tutor\helloworld.dev and click OK. It is not necessary to select or fill other things, at least for an easy start. Then a blank page appears in left pane. On Project menu, click Project Options. In Compiler, type -mno-cygwin -fnative-structand append to it the contents ofheader.txt, created in section Use of pkg-config. See note about-fnative-struct. In Linker Options/Optional Libs or Object Files, past contents of linker.txt, created in section  Use of pkg-config. Click OK. (This process should be repeated per project). In blank page in left pane, you can Copy-Paste hellworld program from 'Getting Started' page at http://gtk.org/tutorial/. OnExecute menu, click Compile. Oops! Save File dialog box appears. In File Name, type c:\tutor\hello.c. Click Save. (I have to wait a little to complete the compilation process). The process until will create helloworld.exe in folder C:\tutor. On Execute menu, click Run. Err! you will get missing 'xxxx.dll' message. This means that path of DLLs that should be linked (dynamically) during runtime is not known. There are two solutions. First one is simple and have to be done only once (not for every project). But second one, called 'Packaging', has usage outside Dev-C++.

First solution is, as suggested by Kylek  in his mini tutorial.

On Tools menu, click Compiler Options. Click on Directories and then Binaries if not selected by default. In bottom blank space, type C:\GTK\runtime\lib (as per our example) and click Add. click OK. On Execute menu, click Run.

Packaging

(excerpt from 'Packaging' section at http://www.dropline.net/gtk/support.php)

Since GTK+ doesn't install to a system directory by default, it will be necessary to include its installation location in your application's path. To do this, read the HKEY_LOCAL_MACHINE\Software\GTK\2.0\Path registry key, append "\lib" to the result, and write out the final value to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\TheNameOfYourExeFile.exe.

Let us see what We have to do in our example. Click Start, and then click Run. In Open, type regedit. Click OK. This will open window named 'Registry Editor'. In left pane, click '+' sign in front of HKEY_LOCAL_MACHINE to expand it. Then  proceed similarly with Software then Microsoft then Windows then CurrentVersion then App Paths. After expanding App Paths, right-click on App Paths then select New and  clickkey. Type helloworld.exe. Press Enter key. You can see in right pane the string '(default)' under the column Name. Right-click '(default)' , then click Modify. In Value data, type C:\tutor\helloworld.exe. Click OK. Now right-click 'helloworld.exe'  in left pane, then select New and click String Value. Type path. Press Enter key. Right-click 'path' , then click Modify. In Value data, type C:\GTK\runtime\lib. Click OK. Close the 'Registry Editor' window.

After completion of above procedure, you can run helloworld.exe in three ways.
  1.  On Execute menu, click Run (in Bloodshed Dev-C++, continuing process we paused).
  2.  Click Start, and then click Run. In Open, type helloworld.exe.
  3.  In Windows Explorer, open folder C:\tutor and double-click helloworld.exe.
(For Bloodshed Dev-C++ 5 beta 6 release 4.9.6.0, first way above doesn't responds to 'Packaging'.)

The Registry entry need to be done only once per project. You can modify the project later  and build it and run it without touching registry again. Note that, the name and location of of .exe file depends upon name and location of .dev file, entered while creating New Project. In our example, we have entered C:\tutor\helloworld.dev. If we have entered C:\temp1\myworld.dev and done remaining procedure without change then Dev-C++ will create myworld.exe (instead of helloworld.exe) in folder C:\temp1 (instead of C:\tutor). And in Registry, we have to make key myworld.exe (instead of helloworld.exe) and its '(default)' string should have to contain C:\temp1 (instead of C:\tutor). But string 'path' will remain unchanged.

Note: When you have to run helloworld.exe in DOS, the path of runtime library must be in PATH variable. The execution does not depend upon registry entry.

Further Development and Comments

I am keeping this document in public domain for further development. I am not going to maintain or develop further this HowTo. As I have said earlier, this HowTo is created out of  help to my friend. I want to make career in another field (Embedded systems) in which GUI programming is useful only in Development phase. I am also novice in that field. And I have to study much important things, other than GUI programming, as early as possible. So I request you to further develop this tutorial , filling the gaps remained. One of the gap, I want to point is 'Basics of Make' mentioned in page above. Also I do not know much about -mno-cygwin -fnative-struct -mms-bitfields.  Next thing I do not know is  directory-structure of GTK+ Development Environment and Runtime Environment. So it would be easy to solve problems with linking or other possible things. I think it is job of  developers who have ported GTK+ to Windows. Also other job they are missing is to provide a Quick-Start tutorial. I do not know they have done it or not but if they have done it then they should provided it at base directory of installation or produce suitable links at Desktop. Otherwise situation may become so chaotic that I and my friend were trying to use runtime libraries  for compiling helloworld.c in Windows. I got them on magazine CD with no information that they are runtime. We do not know how to link DLLs. So I searched the net for 'using DLL' and read some documents on it. Just before concluding that it is not possible to link DLLs without .def files, I luckily  bounced on http://www.dropline.net and understood that I require Development Environment. However, things didn't work initially. And I searched and tried more causing this document. That's all, folks! Have a nice day!



This document is made using Mozilla 1.1.