在静态库之外抛出C ++异常? [英] Throwing C++ exceptions outside static library?

查看:62
本文介绍了在静态库之外抛出C ++异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,异常不得传播模块边界,例如Herb Sutters C ++编码标准(项目62)中所述。当使用其他编译器或仅使用编译器设置进行编译时,这可能会崩溃。

As a rule, exceptions must not propagate module boundaries as for example explained in Herb Sutters C++ Coding Standards (item 62). When compiled with different compilers or just compiler settings this might crash.

我可以理解问题,例如动态链接库。但是我想知道它是否也适用于静态库。就上述规则而言,静态库是模块吗?如果使用其他编译器设置(例如对齐方式)编译了库,并且如果将异常从静态库中抛出并被应用程序捕获,则程序可能会崩溃?

I can understand the issue in case e.g. of dynamic link libraries. But I wonder whether it also holds for static libraries. Is a static library a module in the sense of the above rule? If the library is compiled with other compiler settings (e.g. alignment) might the program crash, if an exception is thrown out of the static library and caught in the application?

推荐答案

通常,静态库必须由相同的编译器和相同的编译器设置(主要)进行编译,以与可交付结果(动态库或可执行文件)兼容。

Generally, a static library has to be compiled by the same compiler and the same compiler settings (mostly) to be compatible with the deliverable (a dynamic library or an executable).

然后,您可以将异常引发到静态库的边界之外,因为它与编译器生成的一组.obj文件没有太大区别。而且您显然可以在不同的.obj模块之间引发异常。

You can, then, throw exceptions outside the boundaries of a static library because it's not much different than a set of .obj files your compiler generated. And you obviously can throw exceptions between different .obj modules.

编辑:

总结注释:


  1. 如果使用相同的编译器和用于编译的编译器设置,则只能使用静态库

  2. 您可以在使用相同编译器和编译器设置编译的模块之间引发异常。

  3. 从1)和2)开始,您可以从静态库中引发异常,因为如果使用它,则意味着您使用的是相同的编译器和编译器设置,因此可以引发异常。

这篇关于在静态库之外抛出C ++异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆