I use Linux when I work from home, I’m forced to use a Mac at work (well, I boot up a virtual Linux OS), and I use Windows when I just want to goof around with my computer. So, while most of my work is done on Linux, it’s imperative that my code work on all platforms; just because I could use any of the three.
Traditionally, C required multiple versions of code, protected by
#ifdef
s. This often required multiple versions of code to be written,
depending on the target system, target OS, and compiler being used.
Clumsy and messy system.
C++ too had similar shortcomings. When it came to writing multi-threaded code, I had to choose either Win32 or Posix, and once I made that choice, I was bound by it. Since those were the days when Ubuntu was driving me crazy, I chose Win32. Bad decision.
Every single action that I attempted was compounded by the fact that
Win32 is the worst API ever. How do I lock a mutex? Well, first I
declare a handle, then declare a mutex, then define the handle to point
to the mutex, then attempt to lock the mutex, specifying a timeout
interval, then check to see if the error on the acquisition is
ERROR_SUCCESS
. A crazy system which leads to crazy code.
And that’s not compatible with Posix, which is a much cleaner API.
So, when C++11 was announced, I jumped with joy at the fact that multi-threading support was built into the language, and that the proposed interface was so much similar to the cleaner Posix API. C++11 allows me to get rid of the system dependent multi-threading APIs, and focus on the code at hand that actually solves the problem. Not only that, C++11 specifies memory models for atomic operations; which allows me to atomically load, store and swap values. The only way this would be possible prior to C++11 was to declare a mutex for every atomic operation. Not a good idea, as it would lead to a tonne of mutexes, with large, irrelevant scope.
The other, messier option would be to dive down into the assembly level of the target platform, and write in some assembly to atomically load or store some values.
Combine the improved multi-threading and memory models in C++11 with CMake, and I get a nice cross platform code, which works on multiple platforms; well, almost. To be really sure, I need to test the code on each platform; but it’s relatively harder to mess up, the most chances occur in CMake, where I need to define compiler options for different build environments using a number of conditional statements. It’s worse, because CMake is scripted, and that means that there may be conditions which are written syntactically incorrect, but I would not be aware of this until I actually tried to build on a system that leads to those conditions. Still, CMake does not, or should not make a bulk of the code.
The usefulness of writing platform independent code was apparent when I worked on EmoDetect. I used Linux (Ubuntu 12.04), Abhinandan used Mac and Ubuntu (12.10), and both Rishabh and Aayush used Windows (different versions here). Yet, we could collaborate perfectly (again, almost; the three of them had an inexplicable aversion to git, so we ended up passing files (not just patches (OMG!))).
I’d say that it’s so important to write platform independent code. I’ve been trying (unsuccessfully) to port Darktable to Windows; and while I’m sure that it would not be too much effort to port the actual DT code, I’m stuck with compiling libraries, all of which were written for GNU, and Windows support was added later as a hack. Some of them don’t compile, many need to be fixed, and that’s holding up the process indefinitely.
That and the fact that I now almost always use Ubuntu, which means that I don’t really bother about Windows software any longer.