我可以禁用 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.
相关文章