如何导出 C++函数作为抛出异常的dll?
当我尝试将以下函数导出为 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() 函数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,如果您需要一个可以抛出异常的“C”函数,则必须使用
/EHs
。请参阅:/EH(异常处理模型)< /a>.您需要在 VisualStudio 项目中进行设置。相反,
/EHc
告诉编译器假设 extern C 函数永远不会抛出 C++ 异常。并且您的编译器会抱怨您的void some_func()
确实会抛出异常。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 yourvoid some_func()
do throw.如果您决心执行编译器警告您的操作,为什么不直接取消警告呢?
If you are determined to do what the compiler is warning you about, why not just suppress the warning?