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 a BSD-based Unix operating system. I use the PC-BSD distribution of FreeBSD, so this description covers PC-BSD, but the principles should be applicable to any other distribution.
BSD is a fairly simple 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. FreeBSD has two management systems: the package manager and the ports manager.
The first stage is to make sure that Gnu make, C++ compiler and emacs packages are installed. These may all be installed already. Open a terminal window to check by typing each command:
gcc --version gmake --version emacs --version
Note that Gnu make is called “gmake” to distinguish it from the built-in BSD make. This means you will have to remember to use “gmake” not “make” to build programs.
In my case, both emacs and gcc were pre-installed, but gmake wasn’t.
For each command not installed, you can use the package manager to install them. This can be done with the command-line tool:
sudo pkg_add -r gcc sudo pkg_add -r gmake sudo pkg_add -r emacs
This will check the set of packages listed and either report them already installed or install them.
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 – the Unix line-endings version with a .tar.bz2 extension. If you are using an earlier version then 2.9, download the wxGTK distribution. Download and unpack this file wherever you like – the library will be installed into the proper place by the installation process. I unpack it using:
tar -xjvf <filename>
This unpacks it into a directory with the same name as the filename without the extension.
Now 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. This means that the application will include all the wxWidgets 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.
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. Note that you are using the standard “make” not the Gnu “gmake” command. However, either will do the job.
Note that if you get an error “undefined reference to ‘backtrace'”, then this is a persistent bug in the build script for wxWidgets. Manually edit the utils/wxrc/Makefile, and add “-lexecinfo” to the “LIBS” line.
The “sudo” prefix is needed by the “make install” because only “Superuser” can install into system directories.
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:
With the example installation I used for this post, I got the following output:
Default config is gtk2-unicode-static-2.9
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 | +-... | +-project1/ | +-project2/
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 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 have changed the default and makefiles v1.7 onwards reflect that change. If you are using an older version of makefiles, then you can work round this by forcing the build to use Unicode:
IMAGE=controls UNICODE=on include ../../makefiles/gcc.mak include ../../makefiles/wx.mak
Or, simply update makefiles to the latest version.
Now, still in emacs, run make by executing the compile command:
Note that if you are unfamiliar with emacs, “M-x” means Meta-key-x, which on must keyboards is Alt-x.
You need to change the program used to do the compile. The default is “make” but on BSD platforms you need to use “gmake”.
This should build correctly and create a program called controls.
Note that if you get an error “undefined reference to ‘backtrace'”, then this is a persistent bug in the build script for wxWidgets. Edit the Makefile for controls, and add the line:
LOADLIBES += -lexecinfo
before any of the includes.
You can run this by using compile again, but changing the compile command to:
And it will pop up the application main window:
This is the basic setup, but you probably also need to use Version Control, which is covered in a separate article.