Cross Compile Friendly
03/21/2012 § Leave a comment
I’m writing a gnome app to hold all my passwords and have it export its data to an android app, maybe put in some simple sync mechanism that will check periodically that both instances are in sync some how over the net. The first part is done, I have some code written that I will use to store the data in a simple way. I was going to use a berkelydb key/data pair, but I decided that a plain old encrypted file would be better. With that done I needed the encryption piece. As in everything I write I try to keep everything as simple as possible. I found a dandy little rijundael encrpytion class and am adapting it to my needs. One need was the ability to make it portable so I could compile it on Windows, I think a windows version of the app is a good idea, so I need to make the code as portable as possible.
The rijndael code was easily ported, but not as I found it. The author of the orignial code used Microsoft-specific STL with his, and made no provision to have the relevant portions cross-compilable.
The issue here is that Microsoft’s general stl::exception object contains an internal string to hold an error message; you don’t need to derive your own class to use it with the extra capability. Which is nice, although its hardly a task to simply derive your own excception class. Plus its outside the specification, which is clear, we’re meant to derive our own exceptions.
But that’s ok, I’m not exactly sure what the danger is in how Microsoft imlemented it, and its nice to use stuff that’s there. What is not nice is to write some otherwise very useful code using a non-standard class object and then go blithely on your way as though you don’t know or care what anyone else is doing. Unless you live in a dark, Microsoft-licensed cave, its difficult not to see how some code isn’t going to port using these extensions.
And there’s nothing about the code that requires Microsoft-specific technology at all. This a cryptographic solution, not a microsoft one. Should be cross-compilable on any C++ platform. The solution is easy enough, add conditional compiler macros, which I’ve done. In the header that defines the Rijndael class I went ahead and added a new namespace (the class itself was placed in the default namespace, also easily fixed) above it, and derived an exception class from std::exception that implemented the particular features the author wanted out of exception, and viola, all done. Wrap that declaration in ifdef MSVC macros and you have code that should compile on a standard C++98 compiler and a Microsoft one.
The take away from this is its only an effort if you don’t design your code to be portable from the start. Even if you ever only write windows apps, or android apps, or whatever, you never know when your code might be the kernel of a brand new technology, especially if its a generic function, like encrypting data.
This is the code I ended up with. I’ve tested it on both windows and linux and will compile as is. I hope that includes MacOS. I promise I’ll test it as soon as I can…