Structuring cross platform builds

Most of my work is focused on embedded targets, many of which have oddball inconsistencies in either the stdlib or os available. So, if your like me, you’ve had to experience the pain of wading through source that’s become a mishmash of #ifdefs in the name of portability.  If another platform was added, yet another set of #ifdef’s would need to be added to add support, and woe to the develop who had to maintain the codebase.

A straightforward example would be part of the openssl codebase:

In thlock.c, You basically have the following configuration:

Implement pthreads_locking_callback
<blah blah>
#ifdef WIN32
Implement win32_locking_callback
#ifdef SOLARIS
Implement win32_locking_callback
#ifdef IRIX
Implement win32_locking_callback

The above code covers only 4 operating systems in somewhat of a simplistic way, with little margin for wide variation between platforms. With a little imagination, one can also see where additional branches would eCos, uCos, bare-metal (no-os). In particular, the bare metal or embedded os implementation would pull in quite a bit more code, further muddying the waters.

I think there’s a better way to manage this via the build process. The #ifdefs can be replaced by a clean platform-based folder structure.

, another ifdefthe original project are added without Managing cross platform code falls into a couple different camps.  The default mode is to utilize the #ifdef PLATFORM_FOO idiom to manage differences between platforms.

Platform dependency handling can fall into two camps.

The problem – ifdef discussion

Bad Examples

Source tree structure

Marginal to good examples – makes an attempt to factor out os specific code to the os library/


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s