21

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.

Whitewall
  • 597
  • 1
  • 7
  • 18

3 Answers3

10

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.

Rom098
  • 2,445
  • 4
  • 35
  • 52
  • 2
    What I don't understand is when using /MD or /MDd why do we need the _STATIC_CPPLIB. From what I have been able read it causes the code to use libcmt.lib and not msvcrtd.lib. The whole point of using /MD is to have dynamically linked libraries and not static linked libraries. Adding _STATIC_CPPLIB seems to defeat the purpose of using the /MD flag. – gnash117 Jun 21 '12 at 18:07
  • _STATIC_CPPLIB is deprecated in MSVC 2010 so using it with MSVC 2010 will just replace one set of warnings with another. – gnash117 Jun 21 '12 at 20:31
  • gnash117, MSDN for MSVC2008 says when using _STATIC_CPPLIB preprocessor definition your application links libcmt.lib instead of the msvcprt.lib, but still links dynamically to the main CRT via msvcrt.lib. – Rom098 Jun 22 '12 at 22:02
  • And what would happen if stl container doesn't have enough memory, for example? Would it just crash? – sasha.sochka Aug 12 '15 at 01:32
  • @sasha.sochka usually the STL app just calls terminate() if not using exceptions. We need to use another way to handle STL errors for such cases, e.g. http://stackoverflow.com/questions/3870435/handling-stl-errors-without-exceptions – Rom098 Aug 12 '15 at 11:35
  • @sasha.sochka https://stackoverflow.com/a/65513682/10870835 – Chef Gladiator Feb 14 '22 at 10:38
7

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

Edouard A.
  • 6,100
  • 26
  • 31
6

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