Building programs using Gtk# 2

Gtk# is a cross-platform library for graphical interfaces. To use Gtk#, you will need to install it on your system, then configure your build environment to use it.

Installing Gtk# 2

On Linux, I recommend that you use Mono for building Gtk# programs. If you are using Mono on Ubuntu, install the libgtk2.0-cil package:

    $ sudo apt install libgtk2.0-cil

On macOS you don't need to install anything, since Gtk# is already included in Mono and Visual Studio for Mac.

If you are using Windows, visit the Mono for Windows page, then click "Download Mono 32-bit" if you want to build using Mono, or "GTK# for .NET" if you want to build using Visual Studio. Run the downloaded installer.

Configuring your build environment

You will need to configure your build environment so that programs include five libraries: ATK, Cairo, GLib, GDK and GTK. The exact steps depend on your operating system and development environment.

Configuring Visual Studio for Mac, or MonoDevelop

  1. Create a new project, choosing the template "Gtk# 2.0 Project".

  2. In the Solution sidebar, right click on References and select Edit References…

  3. In the window that pops up, check the box beside Mono.Cairo.

You should now be able to build a Gtk# program in the current project.

Configuring Visual Studio on Windows

Be sure that you have first installed Gtk# via the link at the top of this page.

  1. Create a new console project.

  2. In the Solution Explorer sidebar, right click on References and select Add Reference…

  3. In the window that appears, click the Browse… button in the lower right corner.

  4. Navigate to the directory C:\Program Files (x86)\GtkSharp\2.12\lib (or wherever else you installed Gtk#).

  5. Enter the Mono.Cairo directory. Double click on Mono.Cairo.dll.

  6. Click the Browse… button again.

  7. Go up one directory, then descend into the gtk-sharp-2.0 directory.

  8. Double click on atk-sharp.dll.

  9. Click the Browse… button again.

  10. Double click on gdk-sharp.dll.

  11. Click the Browse… button again.

  12. Double click on glib-sharp.dll.

  13. Click the Browse… button again.

  14. Double cliick on gtk-sharp.dll.

  15. Click OK.

You should now be able to build a Gtk# program in the current project.

Configuring Geany or another text editor

This will work on Linux or macOS. On Windows, I have not yet been able to get command-line builds to work, so instead you must use Visual Studio as described above.

First verify that these libraries are present on your system. They are probably at the following paths.

On Linux:

On macOS:

To configure Geany:

  1. Open any C# source file in Geany.

  2. With the C# source file open, choose the menu item Build → Set Build Commands.

  3. A dialog will open. In the section "C# commands", replace the entire Compile and/or Build commands with the following: