Reputation: 85
My question is after a frustration, actually I recently studied the standard C++ IO library. I developed on a Linux machine, so everything was fine. Since I used exception handling for file io (file.exceptions(flags))), which is not supported by older version of GNU C++ compiler. The actual deployment machine has very old version of g++ probably 2.9x something. I am writing a data recorder application, since i wrote a lot of code relying on try-catch pair. What should I do now. I tried declaring an exception inherited from std::exception. It works. Is it a good idea to wrap fstream in a header file. If yes, how should I do it, like inherit, or just wrap?
Upvotes: 4
Views: 196
Reputation: 85
This is my workaround, compatible.h file:
#ifndef __COMPATIBLE
#define __COMPATIBLE
#include "exception.hpp"
#ifdef DEPRECATED_LYNX
namespace util
{
DECLARE_EXCEPTION(_Failure)
}
#define _failure util::_Failure
#else
#define _failure std::ifstream::failure
#endif // DEPRECATED_LYNX
#endif // __COMPATIBLE
This is my coresponding cpp file:
#include "compatible.h"
#ifdef DEPRECATED_LYNX
DEFINE_EXCEPTION(util, _Failure)
#endif
Since I am a newbie, this is just a workaround, I now need to manually throw exceptions so i wrapped up the fstream. Throwing exceptions on badbit, failbit and eofbit. I don't know much how good it is.
Upvotes: 0
Reputation: 1912
Since you're using linux & gcc already, it might be a good idea to start using the GNU autotools. Solving portability problems of this type is one of the core purposes of the autotools.
The autotools will generate a file named config.h, with a set of #defines that indicates the presence or absence of certain features in your environment. (In this case, AC_CXX_EXCEPTIONS
is likely the check you want.) You can then use #ifdef tags to have the preprocessor exclude the code you wrote specifically for compatibility with the old compiler whenever the configure script sees that they are not necessary.
The first time you use autotools is a bit of a stiff learning curve, but that's a one-off time cost. They'll make every future project you embark on much easier to set up. You'll also want to check that your target machine supports the autotools, and if so which version of the tools are supported.
Upvotes: 1