我可以禁用 STL 中的异常吗?
我想禁用在 MSVC 下编译的 C++ 应用程序中的异常。 我已将“启用 C++ 异常”选项切换为“否”,但收到警告,告诉我使用选项 /Ehsc,但我不想这样做。
我的代码中没有 try/catch 块,但我使用 STL。 我发现使用宏定义 _HAS_EXCEPTIONS=0 应该禁用 STL 中的异常,但我仍然收到如下警告:
警告 C4275: 非 dll 接口类 'stdext::exception' 用作 dll 接口类的基础'std::bad_typeid' 请参阅“stdext::Exception”的声明 请参阅'std::bad_typeid'的声明
有什么方法可以关闭 STL 的异常吗?
注意:在我的代码中,我也想关闭 RTTI 选项。 无论 RTTI 打开还是关闭,我都会收到相同的警告。
I want to disable exceptions in my C++ aplication, compiled under MSVC. I hve switched the option Enable C++ exceptions to NO, but I get warnings telling me to use the option /Ehsc, which I dont want to.
I do not have try/catch blocks in my code, but I use STL. I have found that using macro definition _HAS_EXCEPTIONS=0 should disable the exceptions in STL, but I am still getting warning like:
warning C4275: non dll-interface class 'stdext::exception' used as base for dll-interface class 'std::bad_typeid'
see declaration of 'stdext::exception'
see declaration of 'std::bad_typeid'
Is there any way how to switch off the exceptions is STL?
Note: In my code I want to switch off the RTTI options, too. I get the same warnings no matter if the RTTI is on or off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Microsoft STL 支持异常停用。
对于 MSVC STL,定义宏 _HAS_EXCEPTIONS=0 会在将应用程序与 libcmt.lib/libcmtd.lib(/MT 或 /MTd 编译器选项)链接时禁用异常。
如果链接到 msvcrt.lib/msvcrtd.lib(/MD 或 /MDd 编译器选项),则需要再定义一个宏 - _STATIC_CPPLIB。 在这种情况下,请在包含任何 STL 代码之前添加以下行:
或将以下行添加到编译器选项:
请注意,您需要在项目设置中禁用 C++ 异常。
Microsoft STL supports exception deactivation.
For MSVC STL defining macro _HAS_EXCEPTIONS=0 disables exceptions in case you link your application with libcmt.lib/libcmtd.lib (/MT or /MTd compiler option).
If you link with msvcrt.lib/msvcrtd.lib (/MD or /MDd compiler option) you need to define one more macro - _STATIC_CPPLIB. In this case either add the following lines before including any STL code:
or add the following to compiler options:
Please note that you need to disable C++ exceptions in your project settings.
您需要使用支持异常停用的 STL。 这通常是编译时宏定义。
除非我弄错了,STLPort 提供了 _STLP_USE_EXCEPTIONS=0 和 _STLP_NO_EXCEPTIONS。 我不知道程序在这些设置下的行为如何。 ;)
我认为 MS STL 中也有一些隐藏的标志。
EASTL 开箱即用,但禁用了例外:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html
You need to use an STL that supports exception deactivation. This is generally a compile-time macro definition.
Unless I am mistaken, STLPort offers this with _STLP_USE_EXCEPTIONS=0 and _STLP_NO_EXCEPTIONS. I don't know how the programs behave with these settings. ;)
I think there is some hidden flag in the MS STL as well.
The EASTL comes out of the box with exceptions disabled:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html
类型 ID 与运行时类型标识有关。 您可能还想尝试关闭 RTTI。
然而,C++ 标准库的某些部分被指定为抛出异常。 如果你禁用它们,你就会驶入“未定义行为”的浑水。
The type id is to do with run-time type identification. You may want to try turning RTTI off as well.
However, certain parts of the C++ Standard Library are specified to throw exceptions. If you disable them you are sailing into the murky waters of "undefined behaviour".