Feeds

Write Portable Code

Solid principles for C and C++ developers

  • alert
  • submit to reddit

Combat fraud and increase customer satisfaction

Book review Portability is one of those virtues we would all love to have in our code. One of the drivers of Java's popularity was the promise of portability across platforms, and of course that same promise exists in many of the more popular scripting languages, such as Python, Ruby and so on.

However, when it comes to coding in C or C++, portability is much, much harder to achieve, simply because coding in these languages tends to be closer to the hardware. Without a virtual machine to code to, the vagaries of real platforms makes even the best of developers hostage to the operating system and the metal it runs on. However, help and advice is on hand for C/C++ coders in the form of Write Portable Code, which might easily have been entitled How to write portable code in C or C++'.

The book opens with a discussion of what portability means and how difficult it is to take a single platform product and port it across to another platform. Unless, of course, portability has been part of the design thinking from the start. Portability, like security, is better as part of the fundamental design than as an after-thought.

Having explored the underlying concept, the book moves on to examine in more detail the technical and logistical issues that have to be faced. These range from differences in hardware, such as processors and floating point number formats, to differences in file systems, memory addressing, dynamic libraries and so on. Such differences can be a nightmare to deal with, and the book explores a variety of strategies for dealing with them.

One of the key ingredients in cross-platform code is the ability to inspect the current platform. This means much more than just querying to find out about the operating system the software is running under. Issues like big-endian vs. little-endian architectures can make even basic functions difficult. Author Brian Hook provides a partial solution in the form of POSH (Portable Open Source Header), which provides several platform introspection functions.

It's not just platform differences that can trip up the unwary. The book also looks at the logistics of coding in different platforms, from compiler differences to source control to editors, IDEs and other tools. Again, as with the other parts of the book, the author's knowledge and experience shines through, as does his writing skill.

A big plus for the book is the sample application, SAL (Simple Audio Library), which puts into practice the principles that are discussed throughout the book. Full source code for SAL and POSH are available for download, making it relatively easy to get started with code.

While the emphasis is on C and C++, other languages do get a mention. Scripting languages in particular are covered in a separate chapter that looks at JavaScript, Python and Ruby. In particular the use of an embedded interpreter is explored, showing how it can be used to code around platform differences but also shows what the trade-offs are in terms of performance and other factors.

To conclude, this book provides the reader with a mixture of solid principles, concrete examples and an extended example using a portable sound library that illustrates how the principles translate into the real world.

Write Portable Code

Author: Brian Hook

Publisher: No Starch Press

ISBN: 1593270569

Media: Book

Level: Intermediate/Advanced

Verdict: Highly practical and very useful

Buy this book at Cash & Carrion

High performance access to file storage

More from The Register

next story
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Oh no, Joe: WinPhone users already griping over 8.1 mega-update
Hang on. Which bit of Developer Preview don't you understand?
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
Windows 8.1, which you probably haven't upgraded to yet, ALREADY OBSOLETE
Pre-Update versions of new Windows version will no longer support patches
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Ditch the sync, paddle in the Streem: Upstart offers syncless sharing
Upload, delete and carry on sharing afterwards?
prev story

Whitepapers

Designing a defence for mobile apps
In this whitepaper learn the various considerations for defending mobile applications; from the mobile application architecture itself to the myriad testing technologies needed to properly assess mobile applications risk.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
Five 3D headsets to be won!
We were so impressed by the Durovis Dive headset we’ve asked the company to give some away to Reg readers.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.