Building programs using Gtk#

In Programming II we will be using the Gtk# library to write graphical programs. To use Gtk#, you will need to install it on your system, then configure your build environment to use it.

Installing Gtk#

If you are using Mono on Ubuntu, install the libgtk2.0-cil and libcanberra-gtk-module packages:

$ sudo apt install libgtk2.0-cil libcanberra-gtk-module

If you are using another Linux distribution, install the equivalent packages for that distribution (ask me if you are unsure).

On MacOS, or Visual Studio on MacOS, you don't need to install anything, since Gtk# is already included in these environments.

If you are using Windows, click the link "Download Gtk#" on the Mono for Windows page, then run the downloaded installer.

Configuring your build environment

On any system, you will need to configure your build environment so that programs include four libraries: Cairo, GLib, GDK and GTK. The exact steps depend on whether you are using a text editor and building using the csc command line, or are using a full IDE.

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 in another section below.

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:

Configuring Visual Studio on macOS, or MonoDevelop on Linux

  1. Create a new console project.

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

  3. In the window that pops up, check the boxes beside

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 glib-sharp.dll.

  9. Click the Browse… button again.

  10. Double click on gdk-sharp.dll.

  11. Click the Browse… button again.

  12. Double cliick on gtk-sharp.dll.

  13. Click OK.

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