Cross Platform Development – Gnu/Linux Setup

As explained in the introductory post, the aim is to set up a cross-platform development environment using the following tools:

  • Gnu make tools
  • Gnu C++ compiler
  • Gnu Emacs editor
  • wxWidgets framework

This post is about how to setup this development environment on Gnu/Linux. I use the Ubuntu distribution of Gnu/Linux, so this description covers Ubuntu, but the principles should be applicable to any other distribution.

Gnu/Linux is the simplest platform to setup for software development, because most of what you need is either already installed or can easily be installed using the package management tools. On Ubuntu, the Aptitude package manager is used.

Gnu Tools

The first stage is to install the Gnu make, C++ compiler and emacs packages.  These may all be installed already – but the trend with Ubuntu is for it to become more desktop-user friendly and less developer-friendly and I was surprised to find that the C++ compiler is no longer installed by default.

So, installing these packages can be done with a single command:

sudo apt-get install make g++ emacs

The “sudo” command runs the command that follows it in “Superuser” mode, necessary when installing software. It will ask for your password before doing the install. The command being run to do the install is the “apt-get install” command. This will check the set of packages listed and either report them already installed or install them.

GTK

To use wxWidgets, you also need to make sure that the GTK+ library is installed. This is again available as an installable package:

sudo apt-get install libgtk2.0-dev

This will also install a whole load of other packages that GTK+ needs to work.

wxWidgets

There is a version of wxWidgets available thorough the aptitude package manager, so in principle could be installed the same way as all the other tools. However, in my experience this package tends to lag behind the latest release. Also, there seems to be a lack of backwards-compatible packages – for example, if you had an application that required v2.4 of wxWidgets, you need to be able to install v2.4.

So, my recommendation for installing wxWidgets is to download and compile it yourself.

Start by downloading the latest source code from the wxWidgets website. The link is http://www.wxwidgets.org/downloads/ and at the time of writing the latest version was 2.9.4. Since 2.9 there is a single distribution for all platforms, so download and unpack that. If you are using an earlier version, download the wxGTK distribution. Download and unpack this .gz file wherever you like – the library will be installed into the proper place by the installation process. I unpack it into ~/wxWidgets.

Open a terminal window and change directory to the unpacked wxWidgets directory. Installation requires three commands:

configure --with-gtk --disable-shared
make
sudo make install

The “configure” command scans the system to work out the options required to build wxWidgets on this platform. In this case I am specifying that the GTK+ library should be used and that wxWidgets should be built as static (non-shared) libraries. There are two options here:

  • –disable-shared: means that the application will include the wxWidgets object code, so will be quite big, but will be easy to install on another system since it will not require any wxWidgets libraries to be installed to work.
  • –enable-shared (default): means that wxWidgets will be built as shared libraries, so will not be included in the application. Installation on another machine will require installation of all shared library dependencies as well. Typically this is done by copying the shared libraries into the same directory as the program or by installing wxWidgets on the target machine.

The “make” command compiles the libraries and “sudo make install” installs the libraries and the headers into the appropriate places in the operating system for build tools to find them. The “sudo” prefix is needed because only “Superuser” can install into system directories.

If you do choose to build the shared version as well, you can repeat the same sequence of commands but with shared libraries enabled:

configure --with-gtk
make
sudo make install
sudo ldconfig

As this shows, you must also run “sudo ldconfig” after “sudo make install” to ensure the system knows about the new shared libraries. Both installations co-exist and the right one is chosen by the application’s Makefile – see later.

Once you have at least one version of wxWidgets installed, you can get details of the installed versions of wxWidgets with the “wx-config” command:

wx-config --list

 With the example installation I used for this post, I got the following output:

Default config is gtk2-unicode-static-2.9
Alternate matches:
  gtk2-unicode-2.9

Universal Makefiles

You can build wxWidgets applications using modified versions of the samples provided in the wxWidgets download. However, they can be over-complicated and difficult to adapt. I have developed a set of simple-to-use universal makefile utilities which reduce the Makefile required to build a wxWidgets application to 3-4 lines of code.

The recommended practice for using the makefiles project is explained in the online documentation, but basically it is best to put the makefiles project in a folder alongside the project you are going to use it in:

Development/
  |
  +-makefiles/
  |   +-gcc.mak
  |   +-wx.mak
  |   +-...
  |
  +-/
  |
  +-/

Download the universal makefiles project from my website on Sourceforge. Be careful not to click on the quick-link to the STLplus project (which makefiles is a subproject of), but choose the latest version of the makefiles project from the list of versions and download the zip file, which will be called something like “makefiles-01-07.zip” – but choose the latest version. Unzip it in the development folder.

Testing the installation

 To test the installation I am going to set up and build an application based on one of the samples – the “controls” sample – in the wxWidgets release, but using my own Makefile system to build it.

To start off, create a project folder called “samples” next to the “makefiles” folder and then a subfolder called controls:

Development/
  |
  +-makefiles/
  |
  +-samples/
      |
      +-controls/

Now copy the source file for the controls project from the wxWidgets installation at samples/controls/controls.cpp into the empty folder. Also copy the whole “icons” subfolder. Finally, you will need the sample.xpm file from the samples folder. Thus you have the following structure:

Development/
  |
  +-makefiles/
  |
  +-samples/
      +-sample.xpm
      |
      +-controls/
          +-controls.cpp
          +-icons/
              +...

Next, in emacs, create a file called Makefile in the controls folder and add the following to it:

IMAGE=controls
WXSTATIC=on
include ../../makefiles/gcc.mak
include ../../makefiles/wx.mak

The WXSTATIC=on option links against the static wxWidgets libraries. If you have built the shared libraries, you can link against those instead by setting WXSTATIC=off, or by removing it since that is the default:

IMAGE=controls
include ../../makefiles/gcc.mak
include ../../makefiles/wx.mak

Note: this needs makefiles version 1.7 or higher (see the makefiles/version.txt to check this). Earlier versions make the wrong assumption about the default build’s use of Unicode characters when used with wxWidgets later than 2.9. In other words, the wxWidgets project has changed the default and makefiles v1.7 onwards reflect that change.

Now, still in emacs, run make by executing the compile command:

M-x compile

Note that if you are unfamiliar with emacs, “M-x” means Meta-key-x, which on must keyboards is Alt-x.

This should build correctly and create a program called controls. You can run this by using compile again, but changing the compile command to:

make run

And it will pop up the application main window:

Controls Example on Gnu/Linux

Controls Example on Gnu/Linux

This is the basic setup, but you probably also need to use Version Control, which is covered in a separate article.

Leave a Reply