如何将C ++函数导出为抛出异常的DLL?

时间:2010-01-25 19:35:56

标签: c++ dll compiler-warnings dllexport

当我尝试将以下函数导出为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()函数。

2 个答案:

答案 0 :(得分:3)

如果您决定执行编译器警告您的操作,为什么不禁止警告?

#pragma warning(disable: 4247)

答案 1 :(得分:3)

据我所知,必须使用/EHs,以防你需要一个可以抛出的“C”函数。见:/EH (Exception Handling Model)。您需要在VisualStudio项目中设置它。

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