我可以禁用 STL 中的异常吗?

发布于 2024-07-13 21:24:29 字数 444 浏览 7 评论 0原文

我想禁用在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯下孤影 2024-07-20 21:24:29

Microsoft STL 支持异常停用。

对于 MSVC STL,定义宏 _HAS_EXCEPTIONS=0 会在将应用程序与 libcmt.lib/libcmtd.lib(/MT 或 /MTd 编译器选项)链接时禁用异常。

如果链接到 msvcrt.lib/msvcrtd.lib(/MD 或 /MDd 编译器选项),则需要再定义一个宏 - _STATIC_CPPLIB。 在这种情况下,请在包含任何 STL 代码之前添加以下行:

#define _HAS_EXCEPTIONS 0
#define _STATIC_CPPLIB

或将以下行添加到编译器选项:

-D_HAS_EXCEPTIONS=0 -D_STATIC_CPPLIB

请注意,您需要在项目设置中禁用 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:

#define _HAS_EXCEPTIONS 0
#define _STATIC_CPPLIB

or add the following to compiler options:

-D_HAS_EXCEPTIONS=0 -D_STATIC_CPPLIB

Please note that you need to disable C++ exceptions in your project settings.

隐诗 2024-07-20 21:24:29

您需要使用支持异常停用的 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

动听の歌 2024-07-20 21:24:29

类型 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文