5

当我尝试将以下函数导出为 dll 时:

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

Visual C++ 2008 给了我以下警告:

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

我需要 extern "C" 因为我使用 Qt QLibrary 来加载 dll 并解析函数名称。没有 extern "C" 就找不到 some_func() 函数。

4

2 回答 2

4

据我所知/EHs,如果你需要一个可以抛出的“C”函数,必须使用它。请参阅:/EH(异常处理模型)。您需要在 VisualStudio 项目中进行设置。

相反,/EHc告诉编译器假设 extern C 函数从不抛出 C++ 异常。你的编译器会抱怨你void some_func()确实抛出了。

于 2012-11-01T16:31:36.577 回答
3

如果您决心做编译器警告您的事情,为什么不直接取消警告呢?

#pragma warning(disable: 4247)
于 2010-01-25T19:44:18.463 回答