Write standards-compliant C# programs in Linux with DotGNU
Working with the Compiler
For those who speak binary, DotGNU works by transforming bytecode into a simple instruction set that is passed on to a virtual machine to be executed via an interpreter. This design makes DotGNU easily portable and explains the number of supported platforms.
The components you'll use are the ilrun run-time engine, which executes the binaries doled out via the cscc compiler, and cscc some-program.cs, which produces a file called a.out. This file can then be executed with:
ilrun a.out
With the -o switch, you can specify a file name when compiling your program with cscc – for instance:
cscc -o some-program.exe some-program.cs
To print debugging information, use the -v switch while compiling.
If you have to link against a particular library, say System.Drawing, you'll need to point this out to the compiler with the -lLIBRARY switch, which will search for the libraries DLL along the library search path. If you are compiling a GUI program that uses the System.Windows.Forms library, you can also use the -winforms switch, which automatically links all the libraries required to process a WinForms-dependent program. Sometimes you'll have to create your own DLL libraries. The -shared switch will produce these DLLs instead of a .exe.
In addition to C#, the DotGNU compiler can also compile to Java Virtual Machine bytecode with the -mjvm switch. Remember to use the .jar file extension instead of .exe or .dll.
Dive In
After that brief introduction to the compiler and common options, I'll honor the long tradition of coding tutorials by writing a "Hello, World" program in C# (Listing 1) and compiling it with cscc -o hello.exe hello.cs.
Listing 1
Hello.cs
The simple program contains only one method: Main(). Command-line arguments are passed to this method as an array of string objects by way of the System library and its various classes and methods. System.Environment.Exit() exits the program and sends a return code to the shell. The System.Console class interfaces the command line to the program. The Console.Writeline() method writes the greeting to standard output, and Console.Error.Writeline() writes to standard error.
Listing 1 greets the user with a name slipped in as input. But why enter a name for the user when a name is probably already defined on the system? Using the System.Collections library, you can, among other things, display the contents of all the environment variables. If you modify the hello.cs program to use the System.Collections library (Listing 2), you can read the username of the user executing the program with value = Environment.GetEnvironmentVariable("USER"), wherein value is a String type variable.
Listing 2
Hello-Advanced.cs
Get Set GUI
One of the best bits about DotGNU is an implementation of the System.Windows.Forms library that doesn't require translation via other popular toolkits such as Gtk. Much like the Java Swing library, DotGNU's System.Windows.Forms draws its own controls.
To compile the code in Listing 3, use:
cscc -o form.exe form.cs -winforms.
The code displays a simple re-sizable window with the usual minimize, maximize, and close controls.
Listing 3
Using System.Windows.Forms
The next step is to add a menu to this window. The complete listing is available on the magazine website [2], but the most important bits are in Listing 4. A custom class called MyMenu inherits from the System.Windows.Forms.MainMenu class and is used to create the menu items. The menu item variables are of the MenuItem type, which is specifically used to create items within a menu or a context menu.
Listing 4
Advanced Form Controls
The MyMenu() constructor creates the File menu item with the new keyword. Similarly, Listing 4 creates an instance of the three menu items and specifies how they'll appear in the menu. Because I already have the File menu ready, I use the Add() method to add the three menu items to the main menu.
For the first-time GUI programmer, this procedure might be a little overwhelming, but really it couldn't be simpler. The various System.Windows.Forms methods take care of adding GUI functionality to the menu and the items, so that when you compile and run the code, the File menu will function as it does in any GUI application: First you click to display the items, and then you click to fold them back in.
Of course, you still have to associate your menu with your original form code and program event handlers for the various items to get them to work. Also, you'll have to tie these event handlers to the particular menu item's click event. Further on, you have to create dialog boxes and add functionality to them with the use of buttons to save the file and do other tasks.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.