Everyone professes to write portable code, but few programmers actually manage to do it. In most cases, so-called portable code comes out littered with #ifs or #ifdefs (or worse, nested #ifs and #ifdefs), rendering the code illegible and obfuscated. Sadly, the lion’s share of many porting efforts is spent trying to figure out which lines of code are actually being compiled and executed. This wastes time and energy, and can be downright frustrating.
Everyone professes to write portable code, but few programmers actually manage to do it. In most cases, so-called portable code comes out littered with #ifs or #ifdefs (or worse, nested #ifs and #ifdefs), rendering the code illegible and obfuscated. Sadly, the lion’s share of many porting efforts is spent trying to figure out which lines of code are actually being compiled and executed. This wastes time and energy, and can be downright frustrating.
To avoid porting hassles, many developers have switched to scripting languages like Perl or Python that tend to hide system specifics very well. However, if you’re writing the next Perl or Python interpreter, chances are you’re using a language like C or C++ and don’t have the luxury of scripting’s layers of abstraction.
To be sure, writing portable code is hard. Even if you’re just targeting Unix variants, there are a multitude of differences — in hardware, system calls, and programming interfaces — that you have to contend with.
Over the next few months, we’re going to delve into techniques and tools that can help you write and distribute extremely portable code. We’ll look at portable build tools, automatically generated test suites, cross-platform programming, and packaging systems like RPM, used to distribute source and binaries in a uniform way. Ultimately, we’ll see how to create a code release worthy of being called “open source.”
This month, we start the series with writing portable code.
What’s in an OS?
If you’ve been…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: