Tuesday, June 26, 2012

Developing GUI Programs with C/C++ in Linux "Introduction"


An introduction to GUI programming using Glade and Code::Blocks

This is an Introduction to my new book I am writing that will introduce GUI development to programmers trying to explore the Linux world and simplify the transformation for MS Visual Studio programmers that need help and direction to a similar environment in Linux world. In this knol, I will publish my first two chapters from my book trying to get more feed back and input on my topics and contents of the book. Also, This will make this knol a Tutorial for this topic. So, it is a work in progress, if you visit and do not see what you were looking for, please, come back a little later or just send me an email and I will gladly discuss your topic or add it to this Knol.


Chapter Index:

Introduction
Chapter 1
Chapter 2
Chapter 3
Chapter4
Chapter5






Introduction:

  • What resources do you need to have to use this book?
  • Why did this book come to life?
  • What do we know about GNU, Open Source?
  • What about Linux Kernel, Distributions?
  • What are the other option to develop GUI programs in Linux?




Programming in Linux is becoming more and more popular as Linux OS gets more user friendly and more regular home users are comfortable using this new Operating System. The more popular Linux Operating System gets the more demanding on adding programs and tools for end user to use. The more demand on programs the more interest in developers to start learning Linux GUI programming. I started learning about Linux about 10 years ago, and at that time it was very interesting new interface, however, it was very hard to understand and find resources to learn more about it. I struggled to learn about it on my own and installed it on an old extra computer I had just to try to keep up with this new environment. At that time I used Mandriva Linux distribution formerly Mandrake Linux. Few years later and I was shocked by how user friendly Linux became and how easy it is to switch to this OS and say good by to other operating systems that we no longer have to use it. As an embedded system programmer and very well oriented with multi programming languages and environment, I wanted to port all my tools and programs to run on Linux the new amazing Operating System so I can really say “I use Linux as my full time OS”.

This book does not teach you how to become a C or C++ programmer, it will introduce C/C++ and make a great use of them but it assumes that you know a good deal of programming concepts and infrastructure. Instead, this book will introduce the GUI programming concept and provide a step-by-step programming examples, to help programmers who are new to Linux or to GUI programming in Linux, take a big jump into GUI programming world and find the resource that will ease developing of new graphical application in Linux OS environment.





What resources do you need to work with this book?

To make a full use of this book you will need to have some resources available. First of all you need a good working computer with no Operating System “OS” to be able to install Linux on it. Or, you could have a computer with a large hard drive and create two partitions one for your original OS and one to install Linux on it. Or you could have your own way of running Linux OS on a computer. For your reference before you start, you probably should keep in mind that working with a compiler and GUI design you will need a fast enough computer with enough resources such as Memory and drive storage. You should also consider a back device like an external USB drive so you can back up your work on it just in case something happens and your computer gets damaged in anyway, even thought that there is a very small chance this could happen but you do not wont to take a chance of loosing your hard work.
Now, let us talk about what minimum resources you should have on your development computer. That is a pentium 4 with a 3GHz processor and at least 1 GB of Memory and lot of storage space. I believe that this spec is not a budget breaker and very easy to come up with. So, after you have this computer you will need to install Linux on it and that brings up a question, what distribution would I need to install? I will immediately answer saying Ubuntu. This is my favorite distribution and that one I used while working on this book. Now, keep in mind that you should download latest release available for Ubuntu. In this book I used Release 9.10 (karmic) which is the latest Ubuntu release at this time, but when you start working you always should download the latest release as there are always a lot of fixes and enhancements every time a new release is available.
Through out this book, I will be using two important GNU programs, Code::Blocks and Glade. These are the two tools you will be using to develop your programs. So, go a head and install them. Next you should know that these two programs require you to have gcc and g++ installed on your computer, the GNU C and C++ compiler and linker. Also, we are going to use the Gtk+ library to build the GUI interface when working with C programming language, and Gtkmm when working with C++ programming language. So, we need to make sure you install all these components on your Ubuntu machine.

Why did this book come to life?

As a long time C/C++ Windows programmer, I am used to find a lot of resources and references when I need help with one of my new topics that I am working on. So, when I started to work on Linux and get used to the GNU gcc and g++ programming environment I found a lot of documentation that are very useful. I also found a lot tools that we can use to develop these type of programs. But, when I started to look at GUI programming, I started to get a lot of dead ends to resources. I placed to questions on the Internet blogs and groups and I got a lot of different answers to what is considered to be the prober way to create a GUI program under Linux. I began to read a lot of documentation and blogs and had to make a decision of what I really want to do. Do I want to use an IDE that would do everything for me? Well, this is not Windows. I really should be aware of every part of my code where I can customise it and change it any time I need to. So, what I really need to do is to understand what is needed to be able to develop a GUI program using C or C++ programming language. So far so good. I continue to read more and found out that to develop a GUI program I need to know what programming language I am going to use. If I am going to use C which requires gcc tool as a compiler and a linker, then I will use Gtk+ which is the library that provides us “programmers”with all the APIs to develop this type of GUI programming. If I am going to use C++ programming language it will require g++ tool as a compiler and a linker, then I will use Gtkmm which is the Gtk+ clone but it is customised for C++ program structures.

Well, as a MS Windows long time programmer, I am really used to the idea of having an IDE, Integrated Development Environment, that will contain and manage all my project structure and configuration. So, I looked around to see what IDEs are available and compared their pros and cons. I found a lot of these IDEs like programs and each one of them had different futures and some limitations. One of the important future I was looking for is the auto-complete of keywords and commands which would really save a lot of time when you are writing a lot of code. Customisation is a nice future that can help when you work a lot with the IDE. Other futures like auto save, text formatter, built-in compile and link tools that internally link to GCC is great options too.


What do we know about GNU, Open Source?

GNU is collection of programs and operating systems that are based on the Free Software idea. GNU was first started by RichardStallman and publicly announced on September 27th , 1983. Software began on January 5th ,1984 after Stallman quit his job so his company would not claim ownership of the free GNU. The plan was to create new free software operating system and bring it to our world just like it used to be in the 1960s and 1970 where programmers were free to study the software they were using, share the software with other people, modify the software to fit their needs, and free to distribute what they modified so others can make use of it.

A lot of people get confused with the idea behind GNU free software and do not exactly understand the meaning of it. The free software term here means free in freedom, freedom to know, freedom to have access to the source code and freedom to change/update that source code to fix or add futures to it. However, this freedom is attached to a responsibility. The responsibility is simply to pass that freedom you have to the users of your applications. So, if you distributing modified programs that are based on GNU programs, then you should give users of your program access to the source code and the freedom to copy, distribute and/or modify it. You should let them know of their rights and present the GNU GPL license to them.

The name GNU was chosen for three reasons. The first reason was it is recursive acronym for “GNU's Not Unix”. So, because GNU is Unix like in design but it is free software and has no Unix code. The second reason is GNU is the real word. It is the name of “Wildebeest”. Third it is an old song , “Song of the GNU” which was a funny song about a talking gnu.

The idea of developing GNU systems is based on the “GNU Project”. The GNU project idea is to deliver free programs to the user and give them the freedom to copy, distribute, and/or modify it. However, without a free operating system, users would still have to use a proprietary software to run their computer. So the main task for the GNU project was to develop a free operating system. The operating system was chosen to be Unix compatible because the design was already proven and portable. And more important compatibility will make it easy for users to switch to the new free OS.

The major components of the GNU are GNU Compiler Collection GCC, the GNU C Library glibc, and more but, these two projects we will use a lot in this book. GCC is the main compiler that we will use to compile the programs we have in this book. There are a lot of other programs developed under GNU GPL license such as email programs, Internet browsers, text formatters, communications tools and utilities, and many other applications.

The GCC or GNU Compile Collections is one of the most important tool in the GNU project. Without the GCC the whole idea of free software would not make any since. With this tool, everyone is able to develop, modify source code and build their executable with out the need to go out and buy an expensive proprietary compiler from a third party source. Even-more, for me to publish this book with all the code I have in it, I would have had to go and find a way to make my reader have access to some type of compiler and linker such as buy a licensed right to distribute a copy of XYZ program with my book that will allow my readers to try out my code and experiment with it. But, thanks to the GNU GCC, all I have to do is point you to the Linux distribution and you have everything you need to create, copy, change and compile all the code in this book. GCC is not just C/C++ compiler and linker, it used for few other programming languages such as C sharp, Java, Ada, Fortran, Pascal, VHDL, and more other languages are supported. Also, gcc can target different processor families such as Alpha, ARM, Atmel AVR, Blackfin, HC12, PowerPC, VAX, x86-64, MIPS, and much more.

What about Linux Kernel?

A year ago, I bought a “Eee pc net-book” to be able to take all my work to any place I go. But, as soon as I powered it up and saw MS Windows XP home edition on it, I said, “Noway that is not what should be running on my net-book” so I immediately repartitioned the hard drive and installed windows XP on a small partition and Ubuntu Linux on the other partition. To be honest, there are still some programs that I might need to run under windows and did not want to force my self with no options if I had to run a windows program or hardware that is not quite supported by Linux.

Well, Linux is a free Operating System that was developed by a Finnish computer science student his name is Linus Torvalds in 1991 and was made free to people to use by 1992. Linus was a student at University of Helsinki and was getting tired of the limitations of the Minix OS, so he decided to develop his own new OS that will be much more stable and free to people to use. He never thought that it will be as popular as it is now. Linux is an open source, it provides anyone with all the source code needed to modify, update, and rebuild the whole OS, so it complies with GNU GPL.

Linux kernel is the operating system that everything else run on top of. Linux kernel is release under the GNU GPL license version 2 plus some proprietary licenses. Most of the Linux kernel has full access to the hardware and has futures to handle interrupts correctly. Also, it improves the support for symmetric multiprocessing (SMP) and preemption which improves latency, and increases responsiveness which simply makes it suitable for real time operating system. When we hear the phrase “Linux distribution” that is not the Linux kernel. Linux distributions are a group of applications tools and utilities that run on top of the Linux Kernel. Such applications are word processors, email program, Internet browser, media players, graphics programs, and communications programs. There are currently over six hundred distributions available and about three hundred of them are in active development. Each distribution was created by a person or group of users. Slackware was created by Patrick Volkerding and then maintained by the Development team. On the other hand, OpenSUSE was created by Novell and started as a business only distribution then changed to focus on the home user desktops and workstations. Below is a list of most popular Linux distributions that I can think of:

Distribution
Creator
Producer
First Public Release
Base Distribution
Latest Release Date
Purpose
UbuntuCanonical Ltd.Canonical Ltd.10/20/04Debian10/29/09General
MandrivaMandrake soft S.A.Mandriva S.A.07/23/98Red Hat Linux04/29/09General
OpenSUSENovell Inc.OpenSUSE Project03/00/1994SUSE Linux11/12/09General
DebianIan MurdockDebian Project08/16/93None09/05/09General
FedoraFedora ProjectFedora Project11/05/03Red Hat Linux11/17/09General
SlackwarePatrick VolkerdingDev Team07/16/93SLS08/26/09General
Gentoo LinuxDaniel RobbinsGentoo Foundation, Inc.03/31/02NoneWeeklyGeneral
Dam Small LinuxJohn AndrewsDev Team2003Knoppix11/17/08Portable
YoperAndreas GiradetThe Yoper Team03/05/03None10/03/07General

Ubuntu:
Ubuntu is free GNU Linux distribution. It started by Mark Shuttleworth and then sponsored by Canonical Ltd. It is the most popular distribution available as it focuses on the ease of use, hardware support and availability of most Linux program packages. Ubuntu is driven from Debian distribution and it is different in the way it has a regular releases every 6 months with long time support (LTS) releases every one and a half years. Also, you can run it off the live-CD with having to install it but when you decide to install it, it is a breath. You can start using it right after the install because it installs most common desktop components. Finally, it has a very strong on-line community for references and support.

Debian:
Debian is also a free GNU Linux Distribution. Is very stable, mature and known to have an outstanding package management. It requires some knowledge to Linux though and is very slow on updates and changes. It is very well maintained and supported by the vast popularity and multilingual community. It is very easy to get help from the Debian related forums and mailing lists.

Slackware:
Slackware is the oldest Distribution to come to life. It was started by Patrick Volkerding on July 13th, 1993 and then maintained by the development team. This distribution is designed for users who like to have control and are not afraid of the command line. One of the disadvantages in this distribution is that the the package system does not have list of dependencies which forces the user to install all package dependencies manually.


Fedora:
Fedora is designed to work with home users, programmers, and corporate servers. In this way it will need some customisations to fit the need of each group of users. Fedora has a great desktop tools and management. It is well known of its new tools and bleeding edge technologies. These advantages bring with some disadvantage which is the instability. So, if you want to use the new futures in Fedora, you will need to realise the consequences.


OpenSUSE:
OpenSUSE one of the top distribution for quite a while as it first appeared around 1992 as S.U.S.E. As a business only distribution and now focuses on home users desktop and workstations. OpenSUSE is managed by Novell, and aims to be the most usable and user friendly desktop Linux distribution. Is is very well known by its great configuration tool, YAST. It has a very well designed control center for administration. Users like it because it is one of the simplest distribution to install and configure. It offers a great support and documentation.

Mandriva:
Mandriva is originally was released to public with the name Mandrake Linux. It became very popular among users who want to switch from MS Windows because of its ease and similarity in desktop layout. Mandriva always up-to-date in tools and software which makes it sometimes unstable.

Gentoo Linux:
Gentoo is really known to be all about customisations. This distribution lets you have the option to customise every and each components of the system. It also has a different type of installation process. It will ask you to choose your kernel, cron and event-logging program. The installation is not click-able and will take a long time and knowledge. If you do not like to read and research then you better choose different distribution with a graphical install.

Dam Small Linux:
dam Small Linux, DSL, is meant to be the smallest Linux distribution ever. You could carry a small basic copy of it with you and boot it from a floppy disk, not that we have these available on most computers any more. But if you have an old computer and wanted to use it for a certain purpose with a small and light Linux distribution, your first choice would be DSL. The ISO is just 50Mb, so you can fit it on a business CD. The real future of DSL that you can boot it directly within other operating system such as MS Windows. So you can take it with you and use it on other computers when you are not home. DSL is based on knoppix which is based on Debian. That makes it have a stable system and good with hardware recognition.

Yoper:
Yoper is know to be the most fastest operating system on earth. It is has something in it that makes it fast and responsive out of the box. Yoper also has a very fast installation, you can install this distribution in about 5 to 10 minutes. The problem with Yoper is that it does not have a lot of configurations. The other problem is Yoper team is very small and lacks manpower. The latest release was in July 2007.


What are the other options to develop GUI programs in Linux?

You can basically write C/C++ programs in Linux using an edit program like gedit in Ubuntu and the GNU gcc, Gtk+, and Gtkmm. However, this is not very convenient when you working on a big project that contains a lot of GUI interfaces and multiple source code files. The idea of using an IDE manager is to mange you file structures and contain everything in one container. Also, all your project settings are stored and managed within a configuration window that is simple to understand and change. Even though, some IDE programs would be so complicated that make finding items and settings very hard and confusing. I find Code::Blocks very simple and complete IDE that you can use now for simple projects and later for advanced projects. Code::Blocks has a simple project manager and still very efficient. The compile and link options are easy to find and configure. Code completion is very helpful. Other programs could be very similar but I found Code::Block easier to adapt after working all these years with MS Visual Studio. Other IDE programs are Anjuta, Qt Creator, Kdevelop, Eclipse, Netbeans.



Web Link:
For more information on GNU visit Wikipedia web site http://en.wikipedia.org/wiki/GNU
GNU General Public License GPL http://www.gnu.org/licenses/gpl.html
Mandriva Linux on Wikipedia http://en.wikipedia.org/wiki/Mandriva_Linux
Wikipedia Linux page http://en.wikipedia.org/wiki/Linux
Linux Distributions Wikipedia http://en.wikipedia.org/wiki/Linux_distribution
Glade User Interface Designer Reference Manual http://library.gnome.org/devel/gladeui/3.6/

No comments:

Post a Comment