How to export C++ function as a dll that throws 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 w开发者_JS百科arning:
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 to extern "C" because I use Qt QLibrary to load the dll and resolve the function name. Without extern "C" it can't find the some_func() function.
As far as I know /EHs
must be used in case you need a "C" function that can throw. See this: /EH (Exception Handling Model). You need to set this in your VisualStudio Project.
On the contrary /EHc
tells the compiler to assume that extern C functions never throw a C++ exception. And your compiler complains you that your void some_func()
do throw.
If you are determined to do what the compiler is warning you about, why not just suppress the warning?
#pragma warning(disable: 4247)
精彩评论