Private Social Media
Had it with Facebook and Twitter?

Try out 'Friends and Family' - FnF, an alternative system for use by private groups.

Mobile friendly.

Simple installation designed to work with shared hostings.

No advertising, no annoying notifications.

Demo available here.

Getting Started with Gtkd.


Please use this link for any future visit - I've changed the file name to gsgtkd102.html so that the whole thing will be consistent (refactoring ;=)).

Back in 2011 I wrote a GUI program for Linux using the D programming Language, and the Gtkd D wrapper library around GTK+2. I noticed recently that Gtkd-2 has been created to work with GTK+3, and with the promise of improved shared library support in D, and that, I thought I would revamp my application to incorporate changes I had thought of and to use GTK+3.

Now that was not exactly a walk in the park. There are various breaking changes between GTK2 and GTK3, and quite lot of tedious work was involved making changes to about 50 source files. However, I was back in 2011, and still am, impressed by the ease of use of Gtkd along with The Digital Mars D compiler, and CodeBlocks. So I thought I would write up something that would get a beginner a little way past 'hello world' in that environment.

The Tools

Well for a start, I am working in Ubuntu 12.04 (because it is LTS). I can't tell you how to do this in Windows - I have XP on a box somewhere, but I never use it. This system has GTK+3 installed by default. If your system does not, consult your system documentation to find out how to install it.

I installed CodeBlocks the Ubuntu way:

sudo apt-get install codeblocks
Which gave me version 10.05. I got the Debian package dmd_2.063.2-0_i386.deb from the downloads section of dlang.org (2.064 is current, I'll try that when I've got my app stable using 2.063.) Use the current version. I installed DMD by double-clicking on the .deb file.

I built Gtkd-2. Download the sources - GtkD-2.3.0.zip-from gtkd.org/download.html, then unzip the sources into some suitable directory - /home/you/gtkd say. In a terminal, check that you have GNU make available (just type make) - if you don't, I you can install package build-essential. Cd to the gtkd directory then do:

make -f GNUmakefile
sudo make -f GNUmakefile install
That should give you the last piece that you need.


When you install dmd, you will basically get a copy of the dlang.org website files corresponding to the version you install. The language and library documentation - which you will need at some point are in there. Point your browser at file:///usr/share/dmd/html/d/index.html, and proceed from there to make suitable bookmarks.

For Gtkd-2, the target for your browser will be something like file:///home/you/gtkd/docs/src/gtk/Widget.html, depending on your user name (you), and where you installed the Gtkd-2 stuff (gtkd). The structure of the HTML files there is a bit strange - no index file, but Widget.html is a reasonable starting point. You'll find your way around from there.

Setting Up CodeBlocks.

This is one of those black-art things. It took me a while to get it right, so follow this carefully.

Start CodeBlocks, go to Settings/Compiler and Debugger. Choose Selected Compiler as Digital Mars D, and set it as default.

In Search directories/Compiler, add:

  • /usr/include/dmd/phobos
  • /usr/include/dmd/druntime
In Search directories/Linker, add:
  • /usr/lib/i386-linux-gnu

Go to Linker settings/Link libraries, and add:

  • /usr/lib/i386-linux-gnu/libphobos2.a
  • pthread
  • m
  • rt
  • dl
In that order.

In Other linker options, I have -Xlinker (I don't know if that is needed, I just stopped messing with things as soon as it worked.)

Now from the File menu, choose Save everything if it is available. Then close CodeBlocks and open it again. Check the Compiler/Debugger settings to make sure that the things you entered are still there.

Any other CodeBlocks settings are done at the project level. Save yourself some trouble on that initially by loading the CodeBlocks project included here

Loading the project.

Extract the project into some convenient directory - I'm assuming /home/you/scratch. ~/scratch will then have a subdirectory gsgtkd, containing file gs.cbp. In CodeBlocks, do File/Open, and choose the .cbp file.

If that appears to go well do Build/Build and run. All being well, you'll see a GUI application called "Getting Started with Gtkd". If not, get on the NewsFeed, and we'll sort it out.

The Application Incrementally - 101.

The project you downloaded has (or will eventually have) a bunch of source files - gs1.d, gs2.d, gs3.d ... Initially, the project contains gs1.d. We'll remove that and add others successively as we go along.

The first stab is to simply get a main window position sufficiently far from the dreaded Unity launch bar. It is not quite as simple as 'hello world, but it's still quite small:

module gs;

import gtk.Main;
import gtk.MainWindow;

class GsGtkd : MainWindow
      super("Getting Started with Gtkd");
      setSizeRequest(600, 400);
      move(200, 200);

void main()
   string[] a;
   new GsGtkd();
The module name - gs - would be used if we had other D files that depended on this one. D then requires that you tell it where to find definitions of entities that are not defined within the file. In this case, we import gtk.Main and gtk.MainWindow.

We then define a class - GsGtkd - that is derived from MainWindow. Its constructor (this()) tells the superclass what title to use, positions the MainWindow, and then shows it.

The main() function can assume the existence of the Gtkd class Main. we imported the Main module, and we will get the implementation at link time from the Gtkd library. I've not written main() as you'll see in the Gtkd examples, just to make a point. Main.init() expects to get an array of strings as an argument. It could come from:

void main(string[] args)
   new GsGtkd();
But there's nothing special about the array of strings: main() is just a regular D main() - with or without arguments. However, there is another strange thing about main() here. That is, we create an instance of class GsGtkd, with new, but we never reference it. Main.run() does not take ay arguments - it must assume the existence of some static variable that is initialized to the appropriate MainWindow type by the MainWindow constructor. I presume this is just a convenience thing (I'm checking).

Future Installments.

Promises, promises ... In GsGtkd 102, we'll introduce Layouts and DrawingAreas, and make a simple line drawing in our MainWindow. Exciting eh!

Proceed to 102.