如何导出 C++函数作为抛出异常的dll?

发布于 2024-08-19 07:01:31 字数 489 浏览 5 评论 0原文

当我尝试将以下函数导出为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷︶言冷语的世界 2024-08-26 07:01:31

据我所知,如果您需要一个可以抛出异常的“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 your void some_func() do throw.

颜漓半夏 2024-08-26 07:01:31

如果您决心执行编译器警告您的操作,为什么不直接取消警告呢?

#pragma warning(disable: 4247)

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

#pragma warning(disable: 4247)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文