std::vector 需要具有 dll 接口以供类 'X<T> 的客户端使用.警告

2021-12-25 00:00:00 dll c++

我正在尝试将我的库作为 DLL 导出,但我收到了很多针对使用 std::vector 的特定类的警告:

I'm trying to make my library exportable as a DLL but I'm getting a lot of these warnings for one specific class that uses std::vector:

template <typename T>
class AGUI_CORE_DECLSPEC AguiEvent {
    typedef void (*AguiCallbackFptr)(T arg, AguiWidget* sender);
std::vector<AguiCallbackFptr> events;
public:
    void call(AguiWidget* sender, T arg) const;
    void addHandler(AguiCallbackFptr proc);
    void removeHandler(AguiCallbackFptr proc);
    void removeHandler();
    AguiEvent();
};

我收到如下警告:

警告 57 警告 C4251:'AguiEvent::events' : 类'std::vector<_Ty>' 需要有客户端使用的 dll 接口类'AguiEvent'

Warning 57 warning C4251: 'AguiEvent::events' : class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'AguiEvent'

我试图找到如何正确执行此操作,但 MSDN 的文档仅适用于 Windows,并且我需要它是跨平台的,以便在实际定义 AGUI_CORE_DECLSPEC 时仅执行 MS 特定的操作.

I tried to find how to do this properly but MSDN's documentation is very Windows Only, and I need this to be cross platform so that it only does MS specific stuff when AGUI_CORE_DECLSPEC is in fact defined.

我应该怎么做才能消除这些警告?

What should I do to get rid of these warnings?

谢谢

推荐答案

从 DLL 导出是特定于平台的.您必须为 Windows 修复此问题(基本上使用 declspec(dllexport/dllimport) 在实例化的类模板上)并将所需的代码封装在 Windows 特定的预处理器宏中.

Exporting from a DLL is platform-specific. You will have to fix this for Windows (basically use declspec(dllexport/dllimport) on the instantiated class template) and encapsulate the required code in your Windows-specific preprocessor macro.

我的经验是,从 Windows 上的 DLL 导出 STL 类充满痛苦,通常我会尝试设计不需要的接口.

My experience is that exporting STL classes from DLLs on Windows is fraught with pain, generally I try to design the interface such that this is not needed.

相关文章