First Time Linux

C++

Strange name for a language. C++ is an extremely widespread and popular language, based on the syntax and foundations of the language C but adding the powerful features of object oriented programming. Many of the large software projects that you're used to using (including Firefox, OpenOffice and the Gimp) are written in C++.

Like java, it's strongly object-oriented and some of the syntax is very similar, but unlike java, C++ code is compiled into native code and must be recompiled for each target platform. After compilation you get a real executable, not instructions to be interpreted by a runtime engine.

C++ is based on public, internationally-agreed standards (rather than being owned by a corporation), so compilers can be written by anyone as long as they meet the standards. You can buy a compiler, with a sophisticated development environment, or you can use the free tools such as Gnu's gcc suite and eclipse.

Getting started

Of course the first thing to do is install the tools that we'll need. Just to get going, we'll use a text editor of your choice (either a graphical one like Kate or a console one like vi) and the command line compiler and linker from gnu. The gnu tools can be easily installed if they're not there already, using urpmi - you'll need both gcc and libgcc packages.

First check that the compiler is ready to go:

$ g++ -dumpversion
3.4.3

Now let's use an editor to create our first program file and save it as "hello.cpp" in a new directory:

#include <iostream>

int main()
{
        std::cout << "hello console\n";
        return 0;
}

This uses the standard libraries provided with C++ (contained in the <iostream> package), using the object cout to print a short message to the console. So that's the source code, now let's compile it and link it into a form the computer can execute:

$ g++ -o hello hello.cpp

The parameters "-o hello" tell the compiler what name to give the resulting executable. Without this, it would use some default name, probably the obscure "a.out". With this flag, the resulting file will be called hello instead. Now let's run it - but first a catch. Normally the current directory isn't in the search path of the shell when it looks for executables, so you have to explicitly reference the path in order for the shell to find it.

$ ./hello
hello console
$

Success! Our first executable program!

GUI programming

Console applications are all very well, but there comes a time when only a GUI will do. Unfortunately there is no standard window toolkit which comes with the standard C++ libraries, so you're going to have to choose a third-party add-on offering and learn how to use it. There are several available, so the choice will depend on the target platform(s) as well as the licensing conditions associated with each framework.

Whichever framework you choose, you'll need to install the development packages associated with it. For this example we'll try out Qt, because of its endorsement by KDE and because of the quality of the documentation provided by its developer, Trolltech. For this, as well as the packages qt3-common and libqt3, we'll also need libqt3-devel. These are all available via urpmi, currently at version 3.3.4. Take note that the very latest version is 4.0, so when looking for the Trolltech docs make sure you read the 3.3 version.

Having installed the framework, let's start by writing a very simple Qt application and compiling it. This example is taken from the Qt docs at the trolltech website. Open your editor again, and make a new file called "hello.cpp" in its own directory. Note: Don't call the directory "qt" ! This confuses the compiler no end!

#include <qapplication.h>
#include <qlabel.h>

int main(int argc, char **argv)
{
   QApplication app(argc, argv);

   QLabel *hello = new QLabel("Hello world!", 0);
   app.setMainWidget(hello);

   hello->show();
   return app.exec();
}

This example makes an application with a single label in it, and shows it. It uses the includes for qapplication.h and qlabel.h which the compiler will find and use to make our executable.

Now, instead of using the g++ command directly (and specifiying where to find the Qt files), we'll follow the Qt recipe and make use of the qmake tool to generate a makefile for us. First we generate a project file for qmake, then use that to generate the makefile, and then use make to do the compilation:

$ qmake -project
$ qmake
$ make
g++ -c -pipe -Wall -W -O2 -fomit-frame-pointer -pipe -march=i586 -mtune=pentiumpro  -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt3/mkspecs/default -I. -I. -I/usr/lib/qt3//include -o hello.o hello.cpp
g++  -o hello hello.o    -L/usr/lib/qt3//lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
$

As you can see, this makefile does call g++ but also automates the compilation parameters and passes the directories to search through to find the Qt files. Next time I want to recompile this file, it will be a lot easier to just type make than to try to remember this long call to g++ and where all the directories are. Now when I do a directory listing, I see several files along with my hello.cpp file:

Now we can run our executable and see the result:

$ ./hello

Now, instead of outputting to the console, we get a new window with our label:

screenshot of Qt
Screenshot of our Qt example

As you can see by experimenting, a lot of the default behaviour of the window is already provided by Qt, including resizing the window, minimizing and closing. Obviously it also provides a host of other GUI widgets as well as labels, such as buttons, list boxes and canvas objects. Now the basics are working with Mandriva, the standard Qt docs can be used to develop something a bit more interesting.

Using eclipse for C++

Eclipse (eclipse.org) is a free and open source IDE, originally made by IBM but since spun off into its own identity. It's written in java, and is hence multi-platform, and often used for java development. One of its main strengths is that each of the components can be plugged-in at will to extend and enhance the tools, and you can even write your own eclipse plugins and use it as an application platform.

Java is of course not the only language supported by eclipse, and with a simple download you can extend its abilities to include C or C++ as well. The important name to know is cdt, or "C/C++ Development Tools". Simply follow their instructions to add cdt to your eclipse installation. You need to install a JRE and eclipse first, and then the download happens direct from within eclipse itself - from the menu Help -> Software updates -> Find and install. Full installation instructions can be found at the home page of cdt at eclipse.org/cdt.

Once this is installed, you can create C++ projects, build them and run them inside eclipse, without going to the command line. It also does the syntax highlighting of course, gives search functions and lots of other neat stuff.

More info

Information right from the horses' mouths can be found at gnu.org and at the home page of the designer of C++, Bjarne Stroustrup. Other good references include the faq of the newsgroup alt.comp.lang.learn.c-c++, the directory page at dmoz.org, and the advert-laden about.com. There's some good introductory info from Dr Alex Martin at Queen Mary's University, London (sorry, link no longer available) or if you really want a boatload of links, try robertnz.net.

For the GUI frameworks, see the homepages for Trolltech's Qt and Gnu's Gtk. There are also useful forums around, for example qtforum.org.