As explained in the introductory post, the aim is to set up a cross-platform development environment using the following tools:
- make tools
- C++ compiler
- Emacs editor
- wxWidgets framework
This post is about how to setup this development environment on a FreeBSD-based Unix operating system.Other versions of BSD are probably similar, but I haven’t been able to try them.
This post is an update from the original – I used to use the PC-BSD distribution of FreeBSD, but this is now discontinued so I now use the TrueOS distribution of FreeBSD. This description covers the July 2017 version of TrueOS which is based on FreeBSD v12, but the principles should be applicable to any other recent 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. All the tools required for this configuration are available through the package manager.
It used to be best practice to use entirely Gnu tools to develop on PC-BSD, but with TrueOS, the recommended compiler has changed and it is now best to use the default compiler which is clang. When I tried building wxWidgets with the Gnu compiler, it crashed whereas the clang build went without a hitch.
It is theoretically possible to use the built-in “make” command for building programs, but that is less capable than Gnu make (which is called gmake on BSD systems) and cannot cope with the makefiles that I use, so I use gmake for software development.
The wxWidgets libraries can be built entirely using the FreeBSD built-in compiler tools. On the version of TrueOS I used, these tools were present in a basic clean install and needed no further installs or configuration.
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 3.1. 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. 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 (–disable-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. The “sudo” prefix is needed by the “make install” because only “superusers” can install into system directories.
You can also build the library to use shared libraries using the option –enable-shared which is also the default. If you build a production application this way, you will need to ship the wxWidgets libraries with the application.
./configure --with-gtk --enable-shared make sudo make install
The build process keeps the two builds separate, so after this, both builds will co-exist. When you use wxWidgets in an application, you can choose which build to use.
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-3.1
Once you have the wxWidgets libraries installed on your system, you can then build wxWidgets applications that use those libraries. The problem is that the build scripts for such applications are complicated. You could work round this by using modified versions of the Makefiles used in the samples provided in the wxWidgets download. However, they can also be 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 gzip file, which will be called something like “makefiles-01-11.tar.gz” – but choose the latest version. Unzip it in the development folder.
The makefiles project requires the use of Gnu make. The built-in make system isn’t sufficient for the task. So the next stage is to make sure that Gnu make is installed. It is not installed by default on the TrueOS distribution. Open a terminal window to check by typing the command:
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.
If the command isn’t found, then it needs to be installed. The recommended way to install extra tools on TrueOS is to use the graphical “AppCafe” application, which is to be found on the start menu “Start->Control Panel->AppCafe”.
Using the search option, find, select and install the “gmake” package.
If you don’t want to use AppCafe or need to use the command-line installer, then use:
sudo pkg install gmake
Note: if you read the code of the makefiles project, you will see that it is set up to use the “g++” command, i.e. the Gnu compiler, for C++ compilation. However, this compiler is not installed on FreeBSD and should not be installed. Nevertheless the makefiles system works! This is because on FreeBSD the operating system maps the reference to g++ (and other Gnu compiler tools) onto the built-in clang compiler. No configuration seems necessary to get this mapping to work.
Testing the installation
To test the installation of wxWidgets 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 as installed in the last section.
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 an editor, 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
Now, run make by executing the command:
You need to be aware of which program is used to do the compile. The built-in default is “make” but the makefiles are too complicated for that tool and it will barf on the input. On BSD platforms you need to use “gmake”.
This should build correctly and create a program called “controls”.
The default setup makes this a dynamic application, that is, it uses the wxWidgets shared libraries. To build a static application that links in the wxWidgets static libraries, use the following option:
Note that this option can also be added to the Makefile before the includes and then just “gmake” alone will always build a static version.
You can run the program from the command line by just using the command:
And it will pop up the application main window:
My preference is to use Emacs as my editor and development environment. This is a personal choice and you can see from the description so far that developing portable applications is simple once wxWidgets has been installed. Any development environment can be used for code development.
If you do want to use Emacs, then it needs to be installed since the basic installation of TrueOS does not include it. If you want to check, then open a terminal window to check by typing the command:
If the command is not found, then the recommended way to install new tools is to use the graphical “AppCafe” application, which is to be found on the start menu “Start->Control Panel->AppCafe”.
Search for “emacs” and you will find that this is supplied as a package called emacs with the version number appended – at the time of writing it was “emacs25”. Select and install the latest Emacs package.
If you need to use the command-line installer, then use:
sudo pkg install emacs25
When compiling in Emacs, you can use the command:
If you are unfamiliar with emacs’ conventions, M-x means meta-x where the meta key is usually mapped onto the Alt key on the keyboard.
The default compile command in Emacs is “make”. Remember to change this to “gmake”.
I have described the basic setup which will get you started developing platform-independent code on TrueOS (or any other distribution of FreeBSD), but you probably also need to use Version Control, which is covered in a separate article.