Jam between the IO Legacy C ++ library and the standardized I / O library

My question is after disappointment, in fact, I recently studied the standard library I + C ++. I developed on a Linux machine, so everything was fine. Since I used exception handling for the io file (file.exceptions (flags)), which is not supported by an older version of the GNU C ++ compiler. The actual deployment machine has a very old version of g ++, probably 2.9x. I am writing an application for recording data since I wrote a lot of code based on a try-catch pair. What should I do now. I tried to declare an exception inherited from std :: exception. It is working. It is a good idea to wrap fstream in a header file. If so, how can I do this, for example, inherit or just wrap it?

+4
source share
2 answers

Since you are using linux and gcc already, it might be nice to start using GNU autotools. Solving problems with portability of this type is one of the main goals of auto tuning.

In autotools, a file called config.h will be created with the set #defines, which indicates the presence or absence of certain functions in your environment. (In this case, AC_CXX_EXCEPTIONS most likely you want to check.) Then you can use the #ifdef tags to make the preprocessor exclude code that 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, this is a slightly tough learning curve, but it is a one-time cost. They will make every future project that you embark on, much easier to set up. You will also want to check if your target machine supports autotools, and if so, which version of these tools is supported.

+1
source

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 comparable cpp file:

 #include "compatible.h" #ifdef DEPRECATED_LYNX DEFINE_EXCEPTION(util, _Failure) #endif 

Since I'm a newbie, this is just a workaround, now I need to manually throw exceptions, so I wrapped fstream. Throwing exceptions on badbit, failbit and eofbit. I donโ€™t know how good it is.

0
source

All Articles