Question

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

Was it helpful?

Solution

If you are determined to do what the compiler is warning you about, why not just suppress the warning?

#pragma warning(disable: 4247)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top