...making Linux just a little more fun!

Writing Portable Code in Linux - Three Different Approaches

By Deividson Luiz Okopnik

Considering the number of different platforms and Operating Systems used today, writing code that runs only on a single system, configuration, or platform can drastically decrease the number of people that might get to use your program. To me, that's a waste of good coding.

To avoid this problem, there are several approaches one can take. In this article, we will be presenting three of them: Mono (with MonoDevelop), Lazarus, and Java (with NetBeans). We will see how to do a basic "Hello World" application in each of the methods, and how to run it on Linux and Windows.


"Mono is a software platform designed to allow developers to easily create cross platform applications." - http://www.mono-project.com/

Mono is an Open Source software platform developed as an implementation of Microsoft's .Net Framework. It provides the possibility of executing .Net applications on platforms unsupported by Microsoft; it also gives you the ability to develop .net-independent Mono applications that run on every system Mono supports (Linux, Mac OS X, Windows, etc.)

The main IDE for developing Mono applications is called MonoDevelop - it's an open-source IDE, developed using the Mono platform. It is primarily available for Linux, but can also be compiled and used on Windows and OS/X.



Developing a simple Mono application with MonoDevelop:

Start up MonoDevelop, and in the Welcome screen select "Start a New Project". Then, select "GTK# 2.0 Project", give it a name, select a location where the project will be saved, and click "Next". On the next screen, press "OK" and you will be given an empty project with an empty form.

MonoDevelop interface is pretty simple - on the left of the screen you have a list of the files in your projects, and to the right the contents of the selected file. If you press 'F5', your project will be compiled and executed - if you do that now, you will see a simple, empty form.

Let's try developing our 'Hello World' application. In the file list, double-click "MainWindow.cs" - that will show your empty form and a list of components you can add to it. To add a component, drag it from the list to your form.

Start by adding "Fixed" from the "Container" component group - that will allow you to add normal components, like buttons, to your form. After adding the Fixed Container, add a "Button" and a "Label".

Change the form title to "Hello World", the button text to "Hello", and erase the label text. To do that, first select the form (by clicking on its title), and in the properties panel (just below the components list) click to expand the "Window Properties". Then, select the "Window Title" property and change it to "Hello World".

Now do the same with the button, by changing its "Caption" property (in the "Button Properties" group) to "Hello", and with the label, by changing its "Caption" property to "" (just erase everything that's there).

Now that all the properties ready, let's create the code to make "Hello World!!!" appear when the user clicks the button. Select the button, click on "Signals" (just above the properties list), expand the "Button Signals" group, and double-click on the "Clicked" signal to have the procedure created for you, then double-click on it again to go to it.

You will see the following code:

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)

Change it to the following (note that the button and the label names might change - mine are 'label1' and 'button1', but if you added more than one they might be called 'label2', 'button3', etc. Be sure to change it in the code to reflect your component names):

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
 label1.LabelProp = "Hello World!!!";

Now, our test application is ready to run - just press 'F5' to check it out. If you want to compile it, press 'F8' and a .exe file will be generated inside the 'bin/Debug' folder, where your project was saved. That .exe can be executed on any platform Mono is installed in without any porting. To execute it in Windows, use the following command:

mono "file_name_here.exe"


"Write once, Compile everywhere." - http://www.lazarus.freepascal.org/

Lazarus is a free, Open-Source IDE for the 'free-pascal' compiler (also free and Open Source), that compiles Pascal and Object-Pascal (including Delphi, in most cases) code. The IDE is currently ported to Linux, Windows, and FreeBSD; the compiler runs in many other OSes, including Windows CE and Mac OS/X.



Developing a simple Lazarus application:

When you open Lazarus for the first time, you will be presented with an empty project containing an empty form. You can press 'F12' to toggle the form/code view. On the top of the interface, there's a list with all the currently installed components which you can use to design your program.

Let's start our example by placing a button and a label in the form. To do this, you can double-click the "Ok" button in the component list - that will create a Button on your empty form. Create a Label in the same way - the label is the little white box with an "A" inside it. You can drag the components around on the form to change their locations, or resize them.

Next, we will change some properties for the controls we are using. To do that, click the Button inside the form to select it. When you've done that, its properties will be shown in the Properties List window (by default, it's on the left side of the screen). That window contains all the properties of the currently selected control, and allows you to change their values. Find the "Caption" property of our button and change it to "Hello".

Next, do the same with the Label, and change the "Caption" property to empty (just delete what's in there). Finally, select the form itself (by clicking in an empty space in it) and change its "Caption" property to "Hello World".

Now, with all the properties configured, we can add the code to make things work on our Form. What we will do is make "Hello World!!!" appear in the label when the user clicks the button. To add code to our button, double click on it - that will take you to the code window and create the function that will be run when the user double clicks the button - you just need to add the code for what you want it to do.

To do that, double-click the button on our form - that will bring you to the form's code and will create an empty procedure called TForm1.Button1Click that will be called when the user clicks the button on your form. Change it to something like this (note that the button and the label names might change - mine are label1 and button1, but if you added more than one they might be called label2, button3, etc - change it on the code to reflect your component names):

procedure TForm1.Button1Click(Sender: TObject);
  Label1.Caption:='Hello World!!!';

That is all the code we need, and all it does is setting the Caption property of "Label1" to "Hello World!!!". You can now press F9 to execute the program and see your form in action. To run it on other systems, you just need to download and install Lazarus there, open the project and compile it - simple as that.

To compile your application you can use the Build option, in the Run menu.

Please note that if you are on Ubuntu and install Lazarus from the "Add/Remove Programs" option, you will need to download the fpc-source package (you can find it here ) and install it manually, and then configure Lazarus ( Environment\Environment Options, set FPC Source Directory to /usr/share/fpcsrc/ ) for it to work properly.


"Mature, extremely robust, and surprisingly versatile Java technology has become invaluable in allowing developers to write software on one platform and run it on practically any other platform. . ." - http://www.java.com/en/about/

Java is the result of a combination of two different "things": The Java Runtime Environment (JRE, containing the Java Platform and the Java Virtual Machine) and the Java programming language.

The Java language is a high-level, object oriented programming language that needs to be compiled to what is called Java Bytecode. The JRE is responsible for running the compiled Java code - turning it into the platform's native executable code - and is ported to several different platforms.



Developing a simple Java application:

To develop our Java application on Linux, we will be using an IDE called NetBeans - a very professional IDE that's free and open-source, runs on Linus, Windows, OSX and Solaris - and they even send you a free DVD if you ask ( http://www.netbeans.org/about/media.html) . Just be sure to install the Java SDK before installing NetBeans, so it gets detected during the installation.

To start our sample application, open up NetBeans, start a new project ( File/New Project ), select "Java Desktop Application", give our project a name ("hello") and click "Finish". That will give you a new project, with a form that contains a menu and a status bar.

You can now add a button and a label that we will be using in this project - do that by dragging from the components list to the panel.

Now let's change the button and the label text properties - to do that, first select the Button, find the "Text" property in the list on the right of the interface and change it to "Hello". Then select the Label, find the Text property, and erase everything that's written there.

With that done, it's time to add code on our form to make it work. To do that, select the button, click on the "Events" button (just above the button properties), and click once in the "Mouse Clicked" event. That will create an empty procedure that will be executed when the user clicks the button. To make "Hello World!!!" appear in the label when the user clicks the button, change the empty procedure so it's like this (note that the button and the label names might change - mine are jLabel1 and jButton1, but if you added more than one they might be called jLabel2, jButton3, etc - change it on the code to reflect your component names):

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
        jLabel1.setText("Hello World!!!");

With that done, you can press F6 to run your application. To build the .jar file you can use F11, and to execute your .jar file do

java -jar "file_name_here.jar"

That will work anywhere the JRE is installed.


All of the presented methods have their own advantages and disadvantages, and it's up to you, the developer, to choose which one is the best for your needs, for the software you are developing, and the systems you want it to run on.

It is also important to note that two of the three tools presented here, MonoDevelop and Lazarus, are quite new and may be missing some useful features, but are in constant development, being fixed and improved daily.

Talkback: Discuss this article with The Answer Gang


Deividson was born in União da Vitória, PR, Brazil, on 14/04/1984. He became interested in computing when he was still a kid, and started to code when he was 12 years old. He is a graduate in Information Systems and is finishing his specialization in Networks and Web Development. He codes in several languages, including C/C++/C#, PHP, Visual Basic, Object Pascal and others.

Deividson works in Porto União's Town Hall as a Computer Technician, and specializes in Web and Desktop system development, and Database/Network Maintenance.

Copyright © 2008, Deividson Luiz Okopnik. Released under the Open Publication License unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 148 of Linux Gazette, March 2008