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

siv*_*udh 5 c++ dll compiler-warnings dllexport

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

extern "C" __declspec(dllexport) void some_func()
{
  throw std::runtime_error("test throwing exception");
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*ler 3

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

#pragma warning(disable: 4247)
Run Code Online (Sandbox Code Playgroud)

  • 然后不要使用 extern "C",而是使用 .def 文件强制导出的名称为未修饰的名称(或任何您想要的名称)。 (2认同)