1

I have installed Csharp and Gtk (both 2 and 3) in Debian Stable Linux and I am trying to run a gtk example code file present in folder /usr/share/gtk-sharp2-examples/ . This simple file contains:

// HelloWorld.cs - GTK Window class Test implementation
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001-2002 Mike Kestner

namespace GtkSamples {

    using Gtk;
    using Gdk;
    using System;

    public class HelloWorld  {

        public static int Main (string[] args)
        {
            Application.Init ();
            Gtk.Window win = new Gtk.Window ("Gtk# Hello World");
            win.DeleteEvent += new DeleteEventHandler (Window_Delete);
            win.ShowAll ();
            Application.Run ();
            return 0;
        }

        static void Window_Delete (object obj, DeleteEventArgs args)
        {
            Application.Quit ();
            args.RetVal = true;
        }

    }
}

However, on running it with command csharp, I get following error:

$ csharp HelloWorld.cs
(7,8): error CS0246: The type or namespace name `Gtk' could not be found. Are you missing an assembly reference?
(7,8): error CS0246: The type or namespace name `Gtk' could not be found. Are you missing an assembly reference?
(7,8): error CS0246: The type or namespace name `Gtk' could not be found. Are you missing an assembly reference?
(7,8): error CS0246: The type or namespace name `Gtk' could not be found. Are you missing an assembly reference?
... (many times) ...

I have following versions of Gtk installed:

Package                 Installed       Previous        Now             State
=======================-===============-===============-===============-=====
gtk-sharp2              2.12.40-2       2.12.40-2       2.12.40-2       install
gtk-sharp2-examples     2.12.40-2       2.12.40-2       2.12.40-2       install
gtk-sharp2-gapi         2.12.40-2       2.12.40-2       2.12.40-2       install
gtk-sharp3              2.99.3-2+b1     2.99.3-2+b1     2.99.3-2+b1     install
gtk-sharp3-examples     2.99.3-2        2.99.3-2        2.99.3-2        install
gtk-sharp3-gapi         2.99.3-2+b1     2.99.3-2+b1     2.99.3-2+b1     install

Incidentally, a cobra language code file using Gtk runs correctly on the same system:

(This example is from: http://cobra-language.com/trac/cobra/wiki/GtkSimpleWindow )

@args -pkg:gtk-sharp-2.0  # version 3.0 also works; 
use Gtk
class MyWindow inherits Window
    def onDeleteEvent(obj, args as DeleteEventArgs)
        """ 
        This method is tied to the deleteEvent, and is called when the close 
        button is clicked.  It causes the application to quit.
        """
        Application.quit
    cue init(title as String)
        # pass the title to the parent window
        base.init(title) 
        # set a size for our window
        .setDefaultSize(300, 200) 
        # register method to handle the close event
        listen .deleteEvent, ref .onDeleteEvent 
class MainProgram
    def main
        # initialise the application
        Application.init
        # create an instance of our main window
        window = MyWindow("Cobra's First Window")
        # show all the widgets within the window
        window.showAll
        # start running the application
        Application.run

Where is the problem and how can it be solved? Thanks for your help.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • Unless i am mistaken, you should run the _gsharp_ shell instead of _csharp_ if your .cs script/program uses Gtk. (I don't really know, i am just regurgitating what the _csharp/gsharp_ man page tells in its first or second paragraph.) –  Jun 07 '19 at 23:53
  • Like the csharp compiler says, you need to add an assembly reference for it. This is usually done with `dotnet add package` or `dotnet add project` in .NET Core... likely the latter since you already have gtksharp installed (`dotnet package` is for using NuGet to fetch packages) – Powerlord Jun 07 '19 at 23:57
  • @elgonzo : gsharp command starts an interactive "Mono C# Shell" in Linux. – rnso Jun 08 '19 at 00:07
  • @Powerlord : There is no dotnet command on my Linux system. Here I am using mono software: https://www.mono-project.com/ – rnso Jun 08 '19 at 00:08
  • @mso Ah, OK, I was assuming you were using .NET Core instead of Mono. .NET Core has an entire build toolchain that comes with it so you can handle dependency management in it. – Powerlord Jun 08 '19 at 03:49
  • You probably should refer to its documentation, https://tirania.org/blog/archive/2008/Sep-08.html Things like `LoadPackage ("gtk-sharp-2.0");` cannot be omitted, as C# Shell relies on you to explicitly load dependencies. Most people would compile it with `mcs` or `csc` instead, https://www.mono-project.com/docs/gui/gtksharp/hello-world/ Or use a MonoDevelop GTK# project. – Lex Li Jun 08 '19 at 04:25
  • Thanks. Command: `mcs gtkexample2.cs -pkg:gtk-sharp-2.0` finally works. If you post it as an answer, I will accept it. – rnso Jun 08 '19 at 07:55

0 Answers0