Tag Archives: linux programming

Guide for building C# apps on Ubuntu: Language quickies

Introduction and Background

In the last post, I talked about many things that C# programmers usually look forward to in their IDEs and environment. If you didn’t read the previous post, please read it at Guide for building C# apps on Ubuntu: MonoProject Introduction. In that post, I talked about usual features and tools that C# developers want to use while working on their projects. In yet another post, I talked about using C# for cross-platform development and technically, that post had most of the content for C# programming on cross-platform environment, but in this post I am specifically talking about the Ubuntu environment only. You may want to read that post first, so head over to “Using C# for cross-platform development” and read that post too. I think you will find that post also very much helpful.

C# programming language has already become very popular in programmers, many beginners are taught C# for their object-oriented programming lessons. The concept of object-oriented is very intuitive and understandable, and praising the .NET, it has been made very easy and simple to program an entire object-oriented project in C# to run on any device having .NET framework installed on it.

In this post, I am going to talk about the language quick help toolkit provided in MonoDevelop environment. MonoDevelop has a great toolkit for C# language help in building the small snippets of code blocks in your programs.

Helpful tools and features in MonoDevelop

Let us count the helpful items and features provided in MonoDevelop. MonoDevelop has some great tools and features, available just for language guide and help while development, but however, we will only talk about a few of them in this section as discussing them all won’t be available in this one small post of mine and I will hopefully cover those topics in the book that I am going to write later. Make sure to check that book once it gets released.

1. Language specification

Sometimes, when you are working on a project, the specification for the language is a “must have” part in your “have this” setup! I have myself found it very hard to get the project running when I forget a single piece of code or how to write this and similar things. It just happens, and you have no control over it. However, usually tools do not come with the language specification and language references and guides. MonoDevelop and Mono Project however do come shipped with the language guide for you! You can get the offline version of their documentation on your machine so that you can reference it whenever you want to.

Mono Documentation is the application that contains the references to the technical part of the language, framework and the software platform itself.

Screenshot (5364)
Figure 1: Mono Documentation application in the All Applications list on Ubuntu Studio.

Mono Documentation is an application package downloaded and installed while you are installing the Mono Project. This applications contains almost everything that you would ever need while programming a project on MonoDevelop IDE. The library tooling contains almost everything, a few of them can be enlisted as:

  1. Mono embedding
    • JIT, Object, Reflection and other core features are listed and described here.
  2. Language
    • Language references: Contains the language information, syntax and structures.
    • Exceptions and errors: Compiler errors are also listed here so that you can search for one.
  3. Commands
    • Some commands for the platform itself.
  4. Mono libraries
    • Of course, Mono wouldn’t run without them! They are native Mono assemblies written to make sure that your .NET code runs on the Ubuntu.

You can use this guide, you can even update this guide because Mono Project is open source and relies on help from the community. Each contribution that you make, is pushed to others! “Sharing is caring“, right? 🙂

This is just a guide, so you are going to have the same one there. But I am just going to show you a few of the pages that you would find interesting and “much wanted”.

Screenshot (5367)
Figure 2: Mono Embedding window open in Mono Documentation. 

These are core components and I would personally recommend that you read them out before using Mono Project for development. Having an understanding at core level is very helpful, even while programming C#!

Language specification can also be captured from this application, C# language has been greatly explained in this application. You will find the explanation for everything in C#, from objects, to strings, to classes to structs and to anything that you would find to learn about in C# and while programming an application.

Screenshot (5371)
Figure 3: C# language overview window opened in Mono Documentation.

However, posting an image and description of each of the page would go out of scope here and it would take a lot of time. I am not going to do that, you can surf that yourself.

2. Compile time error specification

Another main thing that C# programmers usually want to get is a compile time error specification. Of course, you can tell by the message, “Constant value 256 cannot be converted to byte“. But, the compiler error for this is, CS0031. For an intermediate programmer (like me!) that can be easily solvable, and maybe generated from the following code:

// Saving the value '256' to byte type.
byte b = 256;

We can solve that easily, by just changing the value to 255 (or less), or by changing the type to integer and so on. However, what in case when you need to get an example of the context when the errors are somewhat complex.

Remember, errors are thrown when you do not follow the rules defined when a compiler is designed and created. Compiler would always follow those rules, you need to stick to them. These documentations would have those rules defined. So, for example for our problem above, the sample error documentation is like below:

Screenshot (5373)
Figure 4: Compile-time error specification window open in Mono Documentation.

This page would contain many examples of the cases when you may raise this compile-time error. You can read this guide, learn what you did wrong and then solve the problem. Although most of the beginners want to ask questions, however that is not a good way of learning. At least, not in my case. I have always prefered learning by doing it wrong again. This way, I could get an idea of fixing my problems easily. This way would:

  1. Give you a better idea of compiler theory; not that subject, instead the way compiler is trying to compile the code.
  2. Show you other scenarios when the code goes wrong.
  3. Teach you how to fix the code.
  4. Give you a few other helpful resources in the code itself.

There are many other error codes discussed and shared about, you may want to find yourself some help and guidance in those guides. MSDN is a great resource for any C# programmer and you can surely find help by trying to Google for the problem and looking for an answer, but I find this method also very much helpful if you are a beginner and want to learn C# programming.

3. Toolbox support

If you have ever built applications for graphical user-interface, then you are already aware of the toolbox. A toolbox is an element that contains the most used controls ready for you to drag-and-drop or double-click to create in your application project’s currently open window. Now, what MonoDevelop has in it is that it also allows you to create code-snippets by double-clicking on the items in the toolbox. You don’t have to write the long codes yourself, you are provided with the templates for the code snippets and you can then allow MonoDevelop to create them up for you.

Just for the sake of example, have a look at this image below:

Screenshot (5376)
Figure 5: List of C# program structures provided in MonoDevelop Toolbox.

Now, if you have ever programmed in C#, you would know that these are all keywords and structures provided by C# to their programmers. You can chose from these and create a new structure at once. MonoDevelop would do the honor to write down the code for you.

For the sake of example, have a look at our class (this class was created in the previous post of this series, you should consider giving it a look), it doesn’t actually have a constructor so we will create a constructor using this toolbox.

Screenshot (5377)
Figure 6: Class without a constructor.

Once we double-click the item from the Toolbox, (ctor is constructor), our class would now have the following code: (Remember to place the cursor where you want to enter this constructor, or any other code-snippet)

Screenshot (5378)
Figure 7: A sample constructor in our class. 

It just added the constructor to the class… We can now refine it to look better or to add the logic that we want, MonoDevelop doesn’t know of the logic so it would just leave that to us. Maybe it is not intelligent enough. 🙂

Screenshot (5379)
Figure 8: A sample constructor with a comment in it.

Looks better now, doesn’t it? There are many other methods, structures, loops and even class templates that you may want to try out. I have just demonstrated the basic one, and I leave the rest to you. Try them all.

4. Unsafe code in MonoDevelop

C# is a high-level language, agreed! But it does allow developers and programmers to go in an unsafe context and work around with pointers, references and other low-level stuff that C and C++ programmers usually brag about. Well, not anymore, MonoDevelop also supports unsafe context and they can use the power of low-level and “on-demand” memory-management!

By default that is not enabled, so if would like to write something like that,

unsafe {
   // Some "unsafe" code here...
}

You will get an error when you would try to compile the code.

Screenshot (5387)
Figure 9: Unsafe context code with error.

You can fix that in the settings for your project. Right under the general settings, select, “Allow unsafe” and you’re good to go then.

Screenshot (5390)
Figure 10: Allowing the unsafe context in MonoDevelop project configuration window.

The code would now execute and run successfully (unless there are other errors in the source code that you may want to fix first!).

Screenshot (5391)
Figure 11: Unsafe context code compiled successfully and application runs. No code in unsafe context, just for demonstration. 

Good to this point, I guess.

5. IntelliSense in MonoDevelop

Just a quick overview of IntelliSense in MonoDevelop for C# programmers. The IntelliSense feature is very friendly feature, in MonoDevelop for C# programmers as it helps them to:

  1. Write long codes, shortly.
  2. Provide suggestions based on current context and the APIs and references selected.
  3. Show errors and warnings.
  4. Show the list of constructors available to the developers and their description.

However, basically, IntelliSense works and shows the code API suggestions, like in the case of a Console.

Screenshot (5393)
Figure 12: IntelliSense showing code suggestions plus the description of the suggested object (functions are also considered to be objects).

You can see how powerful this feature is. It suggests the programmers with the possible candidate for the function or object and also describes the object to them. Share the overloading of the functions and also gives a brief summary of the function. It is deemed helpful in many cases. However, most of you are already aware of these features in your own IDE as every IDE supports this feature.

Finally: A Visual Studio fan? Like me?

If you are a Visual Studio fan, then chances are that you are also not going to enjoy the default Linux look and feel of the IDE and the code structures. Trust me, I also didn’t like it and I wanted to change the look and feel of the code blocks at least because the code was really hard for me to read and understand.

Screenshot (5385)
Figure 13: Default code and syntax highlighting in MonoDevelop.

You can edit the settings in the Preferences window. Find the Preferences window under Edit header and then chose the syntax highlighting.

Screenshot (5386)
Figure 14: Opening up the Preferences in the Edit category. 

You can then find the settings for the syntax highlighting and select Visual Studio there. That would set up the code and syntax highlighting as per Visual Studio look.

Screenshot (5384)

This would then change the theme and you would get the same syntax highlighting as you had in Visual Studio.

Points of Interest

Well, finally I finished up this post, in which I have covered the quick and helpful material for C# programmers in MonoDevelop. Actually, there are plenty of tools and services available to C# programmers in MonoDevelop but for this short guide, I am going to write a few of them and the ones that I find handy in many cases.

I will post a few other posts discussing the features of MonoDevelop for C# programmers, and however, I will also publish a book collective of all of these features and other tips for C# programmers, soon.

Do let me know what you think is missing, or what you think I shared and you didn’t know of. Looking forward to your replies. 🙂

Developing apps for Ubuntu using Ubuntu SDK

Introduction and Background

In this article I am more focussed to write about applications for Ubuntu platform. I have always wanted to write about the platform, SDK, features and more stuff ever since I started programming for Ubuntu, back 3 months ago. The platform is so catchy, even for script-kiddies, that even I was caught by its catchy features. I do not prefer writing apps where I have to use shell scripts too often. But, Ubuntu is an exceptional case for me though. I have not yet tried using C# or the open sourced .NET Core on Ubuntu but I am sure going to try it some day sooner, and I will update the article for that or write a new one for that one section.

There have been many questions related to Ubuntu programming, graphical UIs in Ubuntu, SDK for Ubuntu etc and I always wanted to make sure that I do share some of my knowledge as an article for community but I was always found busy in other projects and blog etc. that I was not able to actually write it. So, since I am free from any other job, project, I guess before starting my Masters in CS course I should write the article for Ubuntu starters. I am sure you would enjoy the SDK and the article as you read it.

Some light on Ubuntu itself

Ubuntu, if you are unaware of what it is, is a Linux (kernel and Debian) based operating system. So right under that GUI a Linux kernel is being used for the underground stuff. Linux kernel maintains the processes and the communication channels between the operating system and the machine hardware. Machine is totally abstracted from the operating system, and kernel is used (along with a few other stuff) to make sure that operating system runs on every (selected or supported) architecture of machines. For example, you can run Ubuntu on a 32-bits and 64-bits, x86 and other architectures. The processes seem to be alike and the applications that you make for one of the machine runs on other machine too which is running Ubuntu. Why? Because Ubuntu runs at a very high-level in the hierarchy, so the programs that run the application are abstracted from the machine. The OS in this state only acts as the virtual machine, with a byte-code (for example!) that runs in it. The byte-code is compiled to native machine code just-in-time! Due to this fact, the code that you write once is executed on every machine because the virtual machine decides which code to be compiled as which instruction. The instructions for machines differ from one to another.

Some introduction of Linux

Ubuntu is a distribution of Linux, Linux was just a kernel, it is not an operating system. And that is exactly the difference between Linux and GNU. GNU was the project that lacked a kernel, until Linus Torvalds created Linux and GNU guys took that open-source kernel (which, still offers royalty to Mr. Linus) and completed their operating system project. Thus, Linux is the kernel, GNU is the operating system. So, if someone asks you to do something on a GNU based system, do not confuse it with “Hey, but I only know how to do that on a Linux“.

For more: Well, that is it for now for Linux. If you want to learn more about Linux itself, go and read

  1. Minix 3
    Background and a few books that you can get about Minix 3.
  2. Linux
    Background and history
  3. GNU

After reading these, you will have an idea of what is Minix 3, how Linux started and how did it complete GNU. 🙂 I won’t talk more on these, and will move on to Ubuntu for now.

Ubuntu—a Linux distribution

Linux alone would be a favorite operating system for the shell-lovers, and those with a craze for command-line interface. But, for ones like me, if there is no graphical user-interface and no way to click a button to open “This PC”, the OS is not “my type”. Right? There are many distributions of Linux kernel, many companies and organizations has invested their money and time in creating an operating system over Linux kernel. Linux kernel allows them to worry about the operating system and the programs and services that it will offer while kernel manages the low-level services, CPU interrupts, RAM management and other facilities. Latest versions of Linux include many bug fixes, security patches and other loophole fixes so that operating systems have to worry about the code on their ends.

Ubuntu is one of those distributions, and is mostly used on personal computers. Ubuntu is based on Linux (according to the website, and yes, many geeks, it is a Debian-based Linux distribution) and yes, you can use the same features that Linux has, those same user accounts and the commands. But Ubuntu just adds a graphical user-interface on top of that, plus a framework and design to support third-party applications to be developed and ported to the devices. Ubuntu is not just a desktop OS, but also provides services for:

  1. Web servers
  2. Smartphones
  3. Tablets
  4. Cloud management
  5. Quite other options are also available because it is not a one machine based OS.

In my opinion, the concept of Windows 10 is much like Ubuntu’s infrastructure. One operating system to rule it all. So the applications that you create on one device are able to run on multiple devices, just the way Windows 10 applications work, they just need a few tinkering and they are ready to go.

As captured from this thread, where Ubuntu’s architecture is discussed about, the following representation may suffice your needs of understanding the architecture.


Figure 1: Graphical representation of Ubuntu OS from a Linux point-of-view. Drawing the schema for every service and process is almost impossible in a single diagram.

Now, as already said, explaining and connecting each and every service provided in Ubuntu OS or the kernel is almost impossible. So, I will leave you with an abstracted overview that the levels come in a way, like, first there is Linux kernel (skip the machine), then a few services that continue to run and communicate with Linux kernel, then a few UI services that render the graphics on screen for the opened windows and tabs, after that comes your application. These services communicate with the kernel and kernel executes the commands on CPU.

Well, on other hand I have created a much simpler diagram that you may find easier to understand also.


Figure 2: An easier to understand, abstracted overview of Ubuntu OS.

In the above image you can get the overview of Ubuntu OS itself, now if that is some-what clear we can now continue to the main topic, Ubuntu SDK. In the next sections I will walk you through using Ubuntu SDK and how to create an application in Ubuntu SDK for Ubuntu OS.

Ubuntu SDK—Qt creator and a bunch of services

Ubuntu SDK is a collection of a few services that every SDK has, Ubuntu is a Linux based system so an IDE is not a fully-integrated IDE as Windows has, Visual Studio. Ubuntu didn’t come with a full-featured IDE, but not anymore. Ubuntu OS also has a full-featured IDE (special thanks to Qt project) which includes:

  1. Text-editor with syntax highlighting.
    Of course most of the programmers need it, the syntax highlighting includes JavaScript, C++, C and many other languages, including the QML (markup language for Qt applications). Qt is a most widely used IDE and framework for cross-platform development. Qt is used for Windows, Linux (Ubuntu, Debian and Android etc), Mac and mobile phone development. So it has to include the syntax highlighting for a wide-variety of languages.

    • C/C++
    • JavaScript
    • QML
    • XML
    • JSON
    • etc…
  2. Compilers for languages.
    Along with the compilers, it also comes with the markup language and JavaScript parsers.

    • QML allows you to integrate the client-side scripting using JavaScript. So the events are handled using JavaScript and the same pattern is used. Plus, the QML markup language is also so much easy that creating the GUI is not at all a problem.
    • The integrated compilers for C/C++ make it easy to run and debug your application instead of having to execute separate commands for each of these processes.
      • Compile the code
      • Parse the markup
      • Assemble the binaries
      • Create a package
      • Find the executables
      • Run them to see what happens.
    • In Ubuntu, you just have to run the application; or debug it.
  3. Debuggers
    Should you run into any trouble with your application or whether you want to try out the application before publishing it, SDK contains the stuff required.

    • Debugger
    • Profiler
  4. Emulators and devices
    SDK also contains the stuff that would help you to create the emulators and devices, on which you can run the applications on. In this guide, I will use the device that I am using to run the app on.

    • You can create emulators for mobiles, desktop and other platform or architecture where you want your application to run.
    • I will be using Desktop one only in this guide.
  5. Source control
    If you are a team of developers working together to create a project, then you would need source control, every team needs it. Ubuntu SDK has a wide variety of source control ranging from git to Bazaar. You can chose the one that suffices your needs.

    • git is a most-widely used source control, it also allows you to host your code over GitHub. It is fast and is widely used by command-line programmers. Plus, there are many guides already provided for you to learn git.
    • Bazaar is another source control which is mostly used in Ubuntu, I never stumbled upon it in Windows. But, I didn’t find a way to create an account so look for that yourself. 🙂
  6. Publishing tools
    Ubuntu SDK also provides you with facility to publish the applications to Ubuntu Software Center. Where your application can be downloaded by thousands of users!
    Note: There is a commercial version of applications provided, but since I am in Pakistan and do not have a PayPal account I have no idea how that would be done, but that is also simple. Just add the details for your account and they will pay you for the revenue that your application will make.

That is just an overview of Ubuntu SDK, but using Ubuntu SDK and installing it just really very easy. I do love Visual Studio, everyone does, but installing Ubuntu SDK is a very simple and easy task. The size of SDK is also very compact, so downloading the binaries won’t take too much time. Installation is also very robust and fast that it doesn’t really matter what you do, it would take at maximum of 10 minutes; depends on your network and machine.

Installing the SDK

I will assume that you are having Ubuntu installed. You can get Ubuntu SDK two ways,

  1. From the Software Center; where you get to only click a button, “Install” and it gets installed.
  2. or… From the terminal. Where the stuff is processed step-by-step and you are aware of how long it will take and what process is being executed.

I will share “how-to” of both of these procedures to make it clear for you, I personally prefer the terminal way of installing the SDK, they are just 3 commands and you get most of the stuff already fixed up for you!

1. Installing using graphical user-interface

First of all, a section for graphical user-interface lovers. The process is very easy, all you need to do is go the Software Center and install the Ubuntu SDK. Using the software center you would have to only click a single button. Rest of the stuff is managed by the software center itself.

Go to the developer tools, right under their find IDEs. Ubuntu SDK is categorized under there.


Figure 3: Ubuntu SDK listed in the IDEs category of Developer Tools section.

You should click on More Info to get more details on this. This window should come up,


Figure 4: Ubuntu SDK details and a button to Quick-Install it.

You should click on Install if you have gone through the details of this software product. Once you click on that, Ubuntu would ask you for your password to perform super-user actions. It would take some time, it requires both Internet and some time. It would have the SDK installed for you!

2. Installing from the terminal

This is my favorite method of installing the Ubuntu SDK. It doesn’t take much time, but is very powerful and simple way, since it only requires a few commands to install the SDK for you!

Open the terminal from your machine and proceed with installing the SDK,

1. Add the SDK release PPA
$ sudo add-apt-repository ppa:ubuntu-sdk-team/ppa

It will prompt you for a password, enter the password for your user account. Once that is done, proceed with the installation of the SDK package.

2. Install the SDK

Execute the following command to install the SDK:

$ sudo apt update && sudo apt install ubuntu-sdk

This would now look for the packages and other resources on the network to download and setup for the SDK. It does take some time and this is the step that will take most of the time. It depends on your network speed so make sure you have a good network, slow would also work, but slowly. 🙂

Once that is finished, there is no other stuff required to worry about. The SDK is set up. Start it, the terminal way.

$ ubuntu-sdk

There you will have the SDK running in front of you. Yes, it is the Qt creator ported with a few more plugins to work with Ubuntu programming.


Figure 5: Ubuntu SDK start up screen.

In the next section I will walk you through different options that Qt creator (Ubuntu SDK) provides you with for developing the Ubuntu applications. The IDE has many features and many templates for applications that you can use to create the applications. The applications are of wide variety.

  1. Console applications
    If you want to build a program that doesn’t need any graphics or rendering, then you can create a simple C/C++ console application and write the logic for your application.
  2. Services or templates
  3. Graphical user-interface apps
    If you want to create applications that make use of graphics, then these are your options.
  4. Non-Qt projects
    When you need to use the native power of Ubuntu, you can create Non-Qt projects and write softwares that make use of native power for better performance.

The applications created in GUI mode, use QML as the markup language for user-interface and JavaScript as the handler for clients’ interactions. C++ is used as a back-end language for processing complex algorithms and structures. In most cases, JavaScript will suffice for example in case of network requests etc. But in cases where you need CPU to be used, you can make use of C++ language at its best!

Points of Interest—future versions of post

Alright, I think this is enough for this post. Until now you have got the basic idea of Ubuntu SDK, Ubuntu and how to build the applications. You may also have got the understanding of SDK IDE’s environment and other basic tools and packages. That was all this post was meant to provide you with.

In next post about Ubuntu SDK, I will show you how to build different applications on Ubuntu platform. Ubuntu is a very powerful platform to build applications on. From console-based, to services to GUI based applications. The GUI based applications, built using Qt are also very easy in Ubuntu SDK. The QML, C++ configuration and all of that stuff, is very easy and native to understand that even if you have never programmed for Ubuntu, or if you are a web developer you will still get your hands on Ubuntu, pretty quick!

See you in the later posts. 🙂