陷入传统 C++ 之间IO库和标准化IO库
我的问题是经过一番挫折之后,实际上我最近研究了标准C++ IO库。我在Linux机器上开发,所以一切都很好。由于我对文件 io 使用了异常处理 (file.exceptions(flags))),旧版本的 GNU C++ 编译器不支持该异常处理。实际的部署机器有非常旧的 g++ 版本,可能是 2.9x 左右。我正在编写一个数据记录器应用程序,因为我编写了很多依赖于 try-catch 对的代码。我现在该怎么办。我尝试声明一个从 std::exception 继承的异常。有用。将 fstream 包装在头文件中是个好主意吗?如果是,我应该怎么做,比如继承,还是只是包装?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是linux & gcc 已经有了,开始使用 GNU 自动工具可能是个好主意。解决此类可移植性问题是自动工具的核心目的之一。
自动工具将生成一个名为 config.h 的文件,其中包含一组#defines,指示您的环境中是否存在某些功能。 (在这种情况下,
AC_CXX_EXCEPTIONS
可能是您想要的检查。)然后,您可以使用 #ifdef 标记让预处理器排除您专门为与旧编译器兼容而编写的代码,只要配置脚本看到该代码他们没有必要。第一次使用自动工具的学习曲线有点僵硬,但这是一次性的时间成本。它们将使您未来开展的每个项目都更容易设置。您还需要检查您的目标计算机是否支持自动工具,如果支持,则支持哪个版本的工具。
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.
这是我的解决方法,兼容的.h 文件:
这是我对应的 cpp 文件:
由于我是新手,这只是一个解决方法,我现在需要手动抛出异常,所以我包装了 fstream。在 badbit、failbit 和 eofbit 上引发异常。我不太知道它有多好。
This is my workaround, compatible.h file:
This is my coresponding cpp file:
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.