How to export a C ++ function as a dll that throws an exception?

When I try to export the following function as a dll:

extern "C" __declspec(dllexport) void some_func() { throw std::runtime_error("test throwing exception"); } 

Visual C ++ 2008 gives me the following warning:

 1>.\SampleTrainer.cpp(11) : warning C4297: 'some_func' : function assumed not to throw an exception but does 1> The function is extern "C" and /EHc was specified 

I need extern "C" because I use Qt QLibrary to load the DLL and resolve the function name. Without extern "C" it cannot find the some_func () function.

+4
source share
2 answers

If you decide to do what the compiler warns you about, why not just suppress the warning?

 #pragma warning(disable: 4247) 
+2
source

As far as I know, /EHs should be used if you need a "C" function that can be selected. See this: / EH (Exception Handling Model) . You must install this in your VisualStudio project.

Conversely, /EHc tells the compiler to assume that extern C functions never throw a C ++ exception. And your compiler complains that your void some_func() is throwing.

+3
source

Source: https://habr.com/ru/post/1299355/


All Articles