如何dllexport派生自std :: runtime_error的类? [英] How to dllexport a class derived from std::runtime_error?

查看:249
本文介绍了如何dllexport派生自std :: runtime_error的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个提供从标准异常导出的异常类的库:

I have set up a library providing an exception class derived from the standard exception:

#include <stdexcept>
#include <string>

class BaseException : public std::runtime_error
{
    public:
        BaseException( std::string const & msg );
};

到目前为止,这么好。在Unix上编译和处理相当不错。现在我正在准备编译成Windows DLL:

So far, so good. Compiles and handles quite well on Unix. Now I am prepping this for compilation into a Windows DLL:

#ifdef WIN32
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT
#endif

#include <stdexcept>
#include <string>

class MY_EXPORT BaseException : public std::runtime_error
{
    public:
        BaseException( std::string const & msg );
};

然而,这让我警告C4275 非DLL接口类'std :: runtime_error'用作DLL接口类'BaseException'的基础

不幸的是,我对Microsoft风格的文档有些过敏:过分冗长,而不是非常重要。它让我完全感到困惑,实际上我期望什么来解决我的问题。

And unfortunately, I am somewhat allergic to Microsoft-style documentation: Excessively wordy, and not very to the point. It keeps leaving me utterly confused as to what is actually expected of me to solve my problem.

你们中有人能启发我吗?我可以放弃基类,但是然后捕捉 std :: runtime_error std :: exception 不会捕获我的自定义异常类,我非常希望这是可能的。那么...?

Can any of you enlighten me? I could just drop the base class, but then catching std::runtime_error or std::exception would not catch my custom exception class, and I would very much prefer this to be possible. So...?

推荐答案

在这种情况下,有几种选择。

There are a few options for you in this type of situation.


  1. 导出。

  2. 忽略它。

  3. li>
  1. Export it.
  2. Ignore it.
  3. In-line it.

重要的是要记住,从dll导出类的正确方式是导出整个类,包括基数和成员。因此,有几种技术,例如 CodeProject上的这个一个,使用接口和适当的工厂创建类(并匹配破坏)。

It is important to bear in mind that the "correct" way to export class from a dll is to export the entire class, including bases and members. For this reason there are several techniques such as this one on CodeProject, that use an "interface" and appropriate factory to create the class (and matching destruction).

在这种情况下,这对您而言并不太有用,尝试导出 std :: runtime_error 可能更加努力并且可能会在以后引入更大的问题。

This is not too useful for you in this situation, trying to export std::runtime_error is probably more effort and likely to introduce even bigger issues later on.

取自 Microsoft Connect网站此处 webarchive ),这些家族错误本质上是噪音;

Taken from the Microsoft Connect site here (webarchive), the family of these errors are essentially noise;


我建议先避免这一点 - 将STL类型放在DLL的界面中,强制您由STL规则(具体来说,您不能混合不同的主要版本的VC,而您的IDL设置必须匹配)。但是,有一个解决方法。 C4251本质上是噪音,可以静音...

I recommend avoiding this in the first place - putting STL types in your DLL's interface forces you to play by the STL's rules (specifically, you can't mix different major versions of VC, and your IDL settings must match). However, there is a workaround. C4251 is essentially noise and can be silenced...

Stephan T. Lavavej(Micrsoft的C ++库的维护者之一)。

Stephan T. Lavavej (one of the maintainer's of Micrsoft's C++ library).

只要编译器选项通过项目是一致的,只是沉默这个警告应该很好。

So long as the compiler options are consistent through the project, just silencing this warning should be just fine.

最后一个选项是定义 BaseException 类内联,而不是导出它。

The final option is to define the BaseException class inline and not export it at all.

根据我的经验,内联选项降级几乎总是最简单的异常类。

In my experience, the inline option landed up almost always being the easiest for exception classes.

VS2015的C ++运行时更改导致更改导出 std :: exception (它不会从运行时导出)。

Changes in the C++ runtime for VS2015, have resulted in changes to the exporting of std::exception (It is not exported from the runtime).

内联选项现在似乎是最适合的时候(你的里程可能会有所不同)。

The inline option now seems to be the most appropriate at this time (your mileage may vary).

这篇关于如何dllexport派生自std :: runtime_error的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆