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.
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.
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.
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:
/usr/lib/mono/4.0-api/Mono.Cairo.dll
/usr/lib/cli/gdk-sharp-2.0/gdk-sharp.dll
/usr/lib/cli/glib-sharp-2.0/glib-sharp.dll
/usr/lib/cli/gtk-sharp-2.0/gtk-sharp.dll
On macOS:
/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.0-api/Mono.Cairo.dll
/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0/gdk-sharp.dll
/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0/glib-sharp.dll
/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0/gtk-sharp.dll
To configure Geany:
Open any C# source file in Geany.
With the C# source file open, choose the menu item Build → Set Build Commands.
A dialog will open. In the section "C# commands", replace the entire Compile and/or Build commands with the following:
on Linux:
csc
/reference:/usr/lib/mono/4.0-api/Mono.Cairo.dll,/usr/lib/cli/gdk-sharp-2.0/gdk-sharp.dll,/usr/lib/cli/glib-sharp-2.0/glib-sharp.dll,/usr/lib/cli/gtk-sharp-2.0/gtk-sharp.dll
"%f"
on macOS:
csc
/reference:/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.0-api/Mono.Cairo.dll,/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0/gdk-sharp.dll,/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0/glib-sharp.dll,/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0/gtk-sharp.dll
"%f"
Create a new console project.
In the Solution sidebar, right click on References and select Edit References…
In the window that pops up, check the boxes beside
gdk-sharp
glib-sharp
gtk-sharp
Mono.Cairo
You should now be able to build a Gtk# program in the current project.
Be sure that you have first installed Gtk# via the link at the top of this page.
Create a new console project.
In the Solution Explorer sidebar, right click on References and select Add Reference…
In the window that appears, click the Browse… button in the lower right corner.
Navigate to the directory C:\Program Files
(x86)\GtkSharp\2.12\lib
(or wherever else you installed
Gtk#).
Enter the Mono.Cairo
directory. Double click
on Mono.Cairo.dll
.
Click the Browse… button again.
Go up one directory, then descend into the gtk-sharp-2.0
directory.
Double click on glib-sharp.dll
.
Click the Browse… button again.
Double click on gdk-sharp.dll
.
Click the Browse… button again.
Double cliick on gtk-sharp.dll
.
Click OK.
You should now be able to build a Gtk# program in the current project.