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

2023-07-02C/C++开发问题
5

本文介绍了我可以禁用 STL 中的异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想禁用在 MSVC 下编译的 C++ 应用程序中的异常.我已将选项启用 C++ 异常切换为否,但我收到警告,告诉我使用选项/Ehsc,我不想使用该选项.

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.

我的代码中没有 try/catch 块,但我使用 STL.我发现使用宏定义 _HAS_EXCEPTIONS=0 应该禁用 STL 中的异常,但我仍然收到如下警告:

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:

警告 C4275:非 dll 接口类stdext::exception"用作 dll 接口类std::bad_typeid"的基础见'stdext::exception'的声明见'std::bad_typeid'的声明

有什么办法可以关闭STL的异常吗?

Is there any way how to switch off the exceptions is STL?

注意:在我的代码中,我也想关闭 RTTI 选项.无论 RTTI 是打开还是关闭,我都会收到相同的警告.

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.

推荐答案

Microsoft STL 支持异常停用.

Microsoft STL supports exception deactivation.

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

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).

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

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

请注意,您需要在项目设置中禁用 C++ 异常.

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

这篇关于我可以禁用 STL 中的异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6