NO

Author Topic: Pelles C and GTK Quick Start Tutorial  (Read 21957 times)

rweidner

  • Guest
Pelles C and GTK Quick Start Tutorial
« on: August 16, 2012, 10:49:29 PM »
I have finally created a simple GTK application using Pelles C.

Setting Up
Download the "All-in-one" developer bundle for windows from the GTK web site.
http://www.gtk.org/download/win32.php

The following is currently the direct link to the bundle.  http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.24/gtk+-bundle_2.24.10-20120208_win32.zip

When I unzipped the bundle, I renamed the GTK directory from it's very long and detailed name to "gtk" and put it on the root of my C:\ drive.  This little tutorial assumes you'll do the same.

The Working Code
Here is the code I compiled and ran. (copied from http://developer.gnome.org/gtk-tutorial/stable/c39.html)

Code: [Select]
#include <gtk/gtk.h>
#include <windows.h>

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
   
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show  (window);
   
    gtk_main ();
   
    return 0;
}

Configure The Project
And here is how the project was set up.
  • Create new console project (I haven't figured out how to create a console free GTK app yet)
  • Open the project configuration tool using the menu option Project->Project Options...
  • Click the "Complier" tab and check the "Enable Microsoft extensions"
  • (Optional) Change "Warnings" to "Level 2"
  • (Optional) Click the "Assembler" tab and set "Debug information" to "Full"
  • (Optional) Click the "Linker" tab and turn on "Verbose"
  • Click the "Linker" tab and add the following Library files: glib-2.0.lib gtk-win32-2.0.lib gdk-win32-2.0.lib gobject-2.0.lib gdk_pixbuf-2.0.lib gthread-2.0.lib gmodule-2.0.lib pango-1.0.lib atk-1.0.lib zdll.lib
  • Click the "Folders" tab and add the following "Libraries":
    • C:\gtk\lib
  • Click the "Folders" tab and add the following "Includes":
    • C:\gtk\include
    • C:\gtk\include\gtk-2.0
    • C:\gtk\include\glib-2.0
    • C:\gtk\lib\glib-2.0\include
    • C:\gtk\include\cairo
    • C:\gtk\include\pango-1.0
    • C:\gtk\lib\gtk-2.0\include
    • C:\gtk\include\gdk-pixbuf-2.0
    • C:\gtk\include\atk-1.0
  • Compile the project
  • Find the exe you just created and copy all dlls from the C:\gtk\bin directory to the same directory of your new exe.  (Overkill I'm sure.)

Guiding Light :)
Here is the reference url that I used when trying to figure this out.
http://www.etechplanet.com/blog/visual-studio-2008-configuration-for-gtk2b-gui-development.aspx

My Shameless Plug
http://www.techport80.com

--
Ron
« Last Edit: August 16, 2012, 10:51:29 PM by rweidner »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Pelles C and GTK Quick Start Tutorial
« Reply #1 on: August 17, 2012, 01:07:16 PM »
Quote
(I haven't figured out how to create a console free GTK app yet)
From link you gave:
Code: [Select]
#include <gtk/gtk.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#ifdef __POCC__
extern int __argc;
extern char **__argv;
#endif

int main(int argc, char **argv);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return main(__argc, __argv);
}
int main(int argc, char *argv[])
{
return 0;
}
or
Code: [Select]
#include <gtk/gtk.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>

#ifdef __POCC__
#define argc __argc
#define argv __argv
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#endif

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show(window);
gtk_main();
return 0;
}

tips:
  • Use project Working directory to point to gtk\bin-dir for dlls when developing/testing program.
    No need for copying dll's when testing program in poide
  • Use macro GTK-DIR = C:\code\gtk-2.12.9# in ppj-file to avoid permanent path to gtk-dir.
    for example:
    INCLUDE = $(PellesCDir)\Include\Win;$(PellesCDir)\Include;$(GTK-DIR)\include#
    LIB = $(PellesCDir)\Lib\Win;$(PellesCDir)\Lib;$(GTK-DIR)\lib#

« Last Edit: August 18, 2012, 09:53:06 AM by timovjl »
May the source be with you

rweidner

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #2 on: August 17, 2012, 05:01:39 PM »
Thanks,  I went with option 1. (With a slight variation)

Code: [Select]
#include <gtk/gtk.h>
int main( int  argc, char *argv[] );

#ifdef __POCC__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
extern int __argc;
extern char **__argv;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
main(__argc, __argv);
return 0;
}
#endif
int main( int  argc,
          char *argv[] )
{
    GtkWidget *window;
    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show  (window);
    gtk_main ();
    return 0;
}

Also, I just wanted to mention, if you are trying to switch to a console free project, one also needs to modify the project.
  • Open the project configuration tool using the menu option Project->Project Options...
  • Click the "Linker" tab
  • Change the Subsytem type to "Windows"

migf1

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #3 on: November 25, 2012, 02:45:39 PM »
Thanks for this thread!

A while back I made a little Tic-Tac-Toe game, using GTK+2 as a GUI, mainly for 2 resons:
a) to freshen up my rusty GTK abilities (had to work with it since v 1)
b) to demonstrate how to use GTK on Windows, using a complete program as an example.

The game was developed on Windows 7, using MinGW32 GCC 4.6.2 as the primary compiler, and Pelles-C 6.0 for verifying the portability of my code.

I had also written a Readme.txt file explaining step-by-step how to setup GTK+2 on Windows, and also how to compile GTK+2 apps using either a Windows port of gcc (like MinGW) or Pelles-C.

You guys may find it useful.

The game, along with its source-code can be downloaded directly from my homepage: http://x-karagiannis.gr/prg/c-prog/c-games/gtk2-tic-tac-toe/

I intended to improve some documented stuff I was not happy about, in a next version, but I never did and I will probable won't either (at least not in the near future).

Nevertheless & despite those stuff, I think the source-code has the potential to be proven useful to anyone looking for a working GTK+ example, compiled with Pelles-C (note though that I haven't tried to compile it with recent versions of Pelles-C).
« Last Edit: November 25, 2012, 02:50:52 PM by migf1 »

ricky

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #4 on: July 14, 2013, 06:12:19 AM »
I have finally created a simple GTK application using Pelles C.

Setting Up
Download the "All-in-one" developer bundle for windows from the GTK web site.
http://www.gtk.org/download/win32.php

The following is currently the direct link to the bundle.  http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.24/gtk+-bundle_2.24.10-20120208_win32.zip

When I unzipped the bundle, I renamed the GTK directory from it's very long and detailed name to "gtk" and put it on the root of my C:\ drive.  This little tutorial assumes you'll do the same.

The Working Code
Here is the code I compiled and ran. (copied from http://developer.gnome.org/gtk-tutorial/stable/c39.html)

Code: [Select]
#include <gtk/gtk.h>
#include <windows.h>

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
   
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show  (window);
   
    gtk_main ();
   
    return 0;
}

Configure The Project
And here is how the project was set up.
  • Create new console project (I haven't figured out how to create a console free GTK app yet)
  • Open the project configuration tool using the menu option Project->Project Options...
  • Click the "Complier" tab and check the "Enable Microsoft extensions"
  • (Optional) Change "Warnings" to "Level 2"
  • (Optional) Click the "Assembler" tab and set "Debug information" to "Full"
  • (Optional) Click the "Linker" tab and turn on "Verbose"
  • Click the "Linker" tab and add the following Library files: glib-2.0.lib gtk-win32-2.0.lib gdk-win32-2.0.lib gobject-2.0.lib gdk_pixbuf-2.0.lib gthread-2.0.lib gmodule-2.0.lib pango-1.0.lib atk-1.0.lib zdll.lib
  • Click the "Folders" tab and add the following "Libraries":
    • C:\gtk\lib
  • Click the "Folders" tab and add the following "Includes":
    • C:\gtk\include
    • C:\gtk\include\gtk-2.0
    • C:\gtk\include\glib-2.0
    • C:\gtk\lib\glib-2.0\include
    • C:\gtk\include\cairo
    • C:\gtk\include\pango-1.0
    • C:\gtk\lib\gtk-2.0\include
    • C:\gtk\include\gdk-pixbuf-2.0
    • C:\gtk\include\atk-1.0
  • Compile the project
  • Find the exe you just created and copy all dlls from the C:\gtk\bin directory to the same directory of your new exe.  (Overkill I'm sure.)

Guiding Light :)
Here is the reference url that I used when trying to figure this out.
http://www.etechplanet.com/blog/visual-studio-2008-configuration-for-gtk2b-gui-development.aspx

My Shameless Plug
http://www.techport80.com

--
Ron

simoneadore

  • Guest
Steps required to run the file from within the business development.
« Reply #5 on: August 27, 2013, 10:30:58 AM »
How to start the program (gtk) by the compiler, thanks.
« Last Edit: September 03, 2013, 08:46:09 PM by simoneadore »

migf1

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #6 on: June 25, 2015, 05:46:25 PM »
Bumping up an old thread, but I've recently set up Windows 8.1 Pro on a borrowed hd, and I'm happy to report that Pelles-C 8.00.60 (x64) still plays nicely with GTK+2 2.24.10 (32bit).

Since no-one has mentioned it so far, I figured it would be useful to see how to compile from the command line, thus with any scripted mean (e.g. make, or a batch file, or from withing a different IDE, etc).

Providing that gtk2-dev (32bit) is already extracted into the C:\gtk2 folder, and both "C:\gtk2\bin" and "C:\Program Files\PellesC\Bin" have been added to the PATH environment-variable of Windows, a gtk2_hello.c can be compiled with Pelles C from the command-line, as follows (adjust the flags and pelles-c's libs to your liking, but try to stay inside the "32bit world", since GTK+2 (64bits) is still marked as buggy officially):

Code: [Select]
cc -x -Tx86-coff -Ot -Ox -Ob1 -W1 -Gd -Ze -fp:precise /Ic:\gtk2\include\gtk-2.0 /Ic:\gtk2\lib\gtk-2.0\include /Ic:\gtk2\include\atk-1.0 /Ic:\gtk2\include\cairo /IC:\unix\gtk2\include\gdk-pixbuf-2.0 /Ic:\gtk2\include\pango-1.0 /Ic:\gtk2\include\glib-2.0 /Ic:\gtk2\lib\glib-2.0\include /Ic:\gtk2\include /Ic:\gtk2\include\freetype2 /Ic:\gtk2\include\libpng14 gtk2_hello.c /LIBPATH:"C:\unix\gtk2\lib\" /LIBPATH:"C:\Program Files\PellesC\Lib\" -LIBPATH:"C:\Program Files\PellesC\Lib\Win\" -subsystem:console -machine:x86 kernel32.lib advapi32.lib gdi32.lib delayimp.lib gtk-win32-2.0.lib gdk-win32-2.0.lib atk-1.0.lib gio-2.0.lib gdk_pixbuf-2.0.lib pangowin32-1.0.lib pangocairo-1.0.lib pango-1.0.lib cairo.lib gobject-2.0.lib gmodule-2.0.lib gthread-2.0.lib glib-2.0.lib intl.lib
Unless I'm doing something wrong, Pelle's cc driver seems to be quite sensitive to the order of the command-line options (hence the appearance of "gtk2_hello.c" in the middle of that huge command, that is, between the header and the library paths).

For a non-console executable, we can change...
Code: [Select]
... -subsystem:console -machine:x86
to...
Code: [Select]
... -subsystem:windows /ENTRY:"mainCRTStartup" -machine:x86 ...
Besides scripts, this can still be useful right from the command line, if for example we define 2 environment-variables, one for cflags and one for linkflags.

Anyway, my only gripe so far is that Pelles-C's IDE does not let me save the source code in UTF-8 (No BOM) format, which appears to be necessary for using non-English string-literals with the GTK+2 runtime (I haven't tried the gettext api yet, though).

For example, the following code prints garbage instead of "Καλημέρα κόσμε!", unless I convert the gtk2hello.c to UTF8 (No BOM) with Notepad++ (or any other measns) before compiling it with Pelles C. Pelles-C saves in UTF8, but in my case it doesn't work.

Code: [Select]
#include <gtk/gtk.h>

int main( int argc, char *argv[] )
{
    GtkWidget *w = NULL;

    gtk_init( &argc, &argv );

    w = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    g_signal_connect( w, "destroy", G_CALLBACK(gtk_main_quit), NULL );

    gtk_window_set_position( GTK_WINDOW(w), GTK_WIN_POS_CENTER );
//    gtk_window_set_decorated( GTK_WINDOW(w), FALSE );

    gtk_container_add( GTK_CONTAINER(w), gtk_label_new( "Καλημέρα κόσμε!" ) );
    gtk_widget_show_all( w );

    gtk_main();

    return 0;
}
gcc provides 2 very convenient command-line options for converting the source codepage to the desired codepage for the executable. They are called: -finput-charset & -fexec-charset, respectively.

AFAIK Pelles-C does not provide something similar... does it? (didn't see anything relative to the documentation).

PS. Btw, by adding the gtk2 binaries folder to the PATH environment-variable of Windows, the gtk2 runtime becomes available everywhere (no need to copy anything to the project folder).

coder_direct

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #7 on: September 03, 2015, 10:50:30 PM »
I have tried on win7 x64 with Pelles C v8.00.60 (Win64) to install gtk v3.6.4 win64 as per following instructions

1 Download gtk v3.6.4 all-in-one bundle for win64 from http://www.gtk.org/download/win64.php at http://win32builder.gnome.org/gtk+-bundle_3.6.4-20131201_win64.zip

2 Place zip file in c:\program files

3 With right mouse button, unzip file contents

4 Change folder name to gtk, so all files are in c:\program files\gtk

5 Add c:\program files\gtk\bin to the windows path with Control Panel > System > Advance System Settings > Advanced > Environment Variables > add at the end ;c:\program files\gtk\bin > OK

6 Open the project configuration tool using the menu option Project > Project Options

7 Click the "Complier" tab and check the "Enable Microsoft extensions"

8 Click the "Linker" tab and add the following Library files
glib-2.0.lib gtk-win32-3.0.lib gdk-win32-3.0.lib gobject-2.0.lib gdk_pixbuf-2.0.lib gthread-2.0.lib gmodule-2.0.lib pango-1.0.lib atk-1.0.lib pangowin32-1.0.lib

9 Click the "Folders" tab and add the following "Libraries":
C:\program files\gtk\lib

10 Click the "Folders" tab and add the following "Includes":
c:\program files\gtk\include
c:\program files\gtk\include\gtk-3.0
c:\program files\gtk\include\glib-2.0
c:\program files\gtk\lib\glib-2.0\include
c:\program files\gtk\include\cairo
c:\program files\gtk\include\pango-1.0
c:\program files\gtk\include\gdk-pixbuf-2.0
c:\program files\gtk\include\atk-1.0
c:\program files\gtk\include\libxml2

11 Start a new project with File > new >  project > win64 console console exe > name:project1

12 Create a source code file with File > new > source code > name:source1

13 Place these contents in the file:
#include <gtk/gtk.h>

int main (int argc, char *argv[])
{
    GtkWidget *window;
   
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show (window);
   
    gtk_main ();
   
    return 0;
}

14. Save with File > save all

15. Build and run with Project > Build and Project > execute



I get the following error:
Building project1.exe.
POLINK: fatal error: File not found: 'pango-.obj'.
*** Error code: 1 ***
Done.

Did I forget to add a pango include?

Thanks
John
« Last Edit: September 04, 2015, 08:04:42 AM by coder_direct »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Pelles C and GTK Quick Start Tutorial
« Reply #8 on: September 04, 2015, 10:11:21 AM »
I use C:\code\gtk3 as development folder and i have no problems with those libraries.
Maybe that C:\Program files\gtk folder is the problem.
I don't test that :)
May the source be with you

coder_direct

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #9 on: September 04, 2015, 02:36:38 PM »
I will try this and see if it makes a difference

Upon installing when the path variable is added, I tried to enter the following

Goto Start > Run > cmd > CTL-SFT-ENT > YES and run the following commands from the windows command prompt as administrator
\program files\gtk\etc\pangopango-querymodules > c:\program files\gtk\etc\pango\pango.modules
\program files\gtk\bin\gdk-pixbuf-query-loaders > c:\program files\gtk\lib\gdk-pixbuf-2.0\2.10.0\loaders.cache
\program files\gtk\bin\gtk-query-immodules-3.0 > c:\program files\gtk\lib\gtk-3.0\3.0.0\immodules.cache

I received for all commands the message "The specified module could not be found". I am wondering if it has anything to do with this

John

coder_direct

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #10 on: September 04, 2015, 09:22:16 PM »
Indeed, moving the gtk+ files to folder c:\gkt worked. Below are the complete instructions

1) Download gtk v3.6.4 all-in-one bundle for win64 from http://www.gtk.org/download/win64.php at http://win32builder.gnome.org/gtk+-bundle_3.6.4-20131201_win64.zip

2) Place zip file in c:\

3) Unzip file contents. With [Right Mouse Button > Extract All > to folder: c:\ > Extract]. You now have all gtk files extracted in c:\gtk

4) In order to set the gtk folder and add the c:\gtk\bin folder to the path, check the path and execute the gtk3 demo program, run the following commands from the windows command prompt as administrator, goto [Start > Run > cmd > CTL-SFT-ENT > YES]
setx GTKDIR c:\gtk /m [ENTER]
set PATH=%PATH%;%GTKDIR%\bin [ENTER]
set
gtk3-demo [ENTER] and close the demo program [X]
exit

5) Start the Pelles C IDE [Start > All Programs > Pelles C for Windows > Pelles C IDE]

6) Open the Pelles C IDE configuration tool using the menu [Tools > Options]. Click the "Folders" tab and click the "Folders" tab and select "Type: executables" and add c:\gtk\bin > OK > OK

7) Start a new project with menu [File > New > Project > Win64 Console Program exe > Name:project1 > OK]

8 ) Create a source code file with menu [File > New > Source Code > Name:source1 > File > Save as File Name: source1 > Save > Yes]

9) Place these contents in the file:
#include <gtk/gtk.h>

int main (int argc, char *argv[])
{
    GtkWidget *window;
   
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show (window);
   
    gtk_main ();
   
    return 0;
}

10) Open the project configuration tool using the menu option [Project > Project Options]

11) Click the "Complier" tab and check in "options" the "Enable Microsoft extensions" and select for "warnings" the "Level1"

12) Click the "Linker" tab and add the following Library files at the end of the ones already in it
glib-2.0.lib gtk-win32-3.0.lib gdk-win32-3.0.lib gobject-2.0.lib gdk_pixbuf-2.0.lib gthread-2.0.lib gmodule-2.0.lib pango-1.0.lib atk-1.0.lib pangowin32-1.0.lib

13) Click the "Folders" tab and add the following "Libraries": c:\gtk\lib

14) Click the "Folders" tab and add the following "Includes":
c:\gtk\include
c:\gtk\include\gtk-3.0
c:\gtk\include\glib-2.0
c:\gtk\include\gio-win32-2.0
c:\gtk\include\cairo
c:\gtk\include\pango-1.0
c:\gtk\include\gdk-pixbuf-2.0
c:\gtk\include\atk-1.0
c:\gtk\lib\glib-2.0\include

15) Save with menu [File > Save all]

16) Build and run with menu [Project > Build] and [Project > Execute]

17) A small window should come-up called "project1.exe". Close it with [X] and close the "console program output" command window [X]

18) To run the program in MS Windows, check the project folder in c:\users\username\my documents\pelles c projects\project1 and double click file project1.exe. A message comes up that libglib-2.0-0.dll is required. Take this file from folder c:\gtk\bin and copy it in the same folder as the project1.exe file and double click it again. You now get the message "the application was unable to start correctly". Therefore, copy file project1.exe to the c:\gtk\bin folder and double click it again. Now it seems to run. Some of the required dll dynamic link library files seem to be present in this folder

If you want a better Windows style theme, edit file C:\gtk\etc\gtk-3.0\settings.ini with
[Settings]
gtk-theme-name = "MS-Windows"
« Last Edit: September 05, 2015, 12:16:53 PM by coder_direct »

migf1

  • Guest
Re: Pelles C and GTK Quick Start Tutorial
« Reply #11 on: September 06, 2015, 11:06:22 AM »
In general, avoid installing mingw, glib, gtk+ and friends in folders whose pathname contain spaces.