【发布时间】:2011-01-07 18:49:38
【问题描述】:
我正在从事一个规模相当大的项目,该项目跨越许多共享库。我们也非常依赖 STL、Boost 和我们自己的模板类和函数。许多导出的类包含模板成员,导出的函数包含模板参数。
这是我如何进行库导出的简化示例:
#if defined(_MSC_VER) && defined(_DLL)
// Microsoft
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#elif defined(_GCC)
// GCC
#define EXPORT __attribute__((visibility("default")))
#define IMPORT
#else
// do nothing and hope for the best at link time
#define EXPORT
#define IMPORT
#endif
#ifdef _CORE_COMPILATION
#define PUBLIC_CORE EXPORT
#define EXTERNAL_CORE
#else
#define PUBLIC_CORE IMPORT
#define EXTERNAL_CORE extern
#endif
#include <deque>
// force exporting of templates
EXTERNAL_CORE template class PUBLIC_CORE std::allocator<int>;
EXTERNAL_CORE template class PUBLIC_CORE std::deque<int, std::allocator<int> >;
class PUBLIC_CORE MyObject
{
private:
std::deque<int> m_deque;
};
SO,我的问题是当我在 Visual Studio(2008 和 2010)中编译时,我收到以下警告:
警告 C4251: 'std::_Deque_val<_ty>::_Almap' : 类 'std::allocator<_ty>' 需要 有 dll 接口可供使用 类客户 'std::_Deque_val<_ty>'
这似乎暗示我没有导出std::allocator<int>,我有。而且这不是我的导出不正确,因为不包括
EXTERNAL_CORE template class PUBLIC_CORE std::allocator<int>;
EXTERNAL_CORE template class PUBLIC_CORE std::deque<int, std::allocator<int> >;
产生警告:
警告 C4251:“MyObject::m_deque”: 类 'std::deque<_ty>' 需要有 客户端使用的dll接口 类“我的对象”
我唯一能想到的是 _Ty 关于 std::allocator 的警告在某种程度上不是 int,但我似乎找不到任何迹象表明它会不是这样,因为std::deque<int> 将在逻辑上分配 std::allocator<int>。
消费应用程序可以很好地使用该类,但我有一种直觉认为不应忽略此警告。在 Linux 中使用 g++ 进行编译时,不会发出任何错误(尽管这并不意味着它工作正常)。 g++ 是否会自动执行 MSVC 无法执行的操作?我一直针对 Linux 上的 GCC、OSX 上的 LLVM 和 Windows 上的 MSVC,但我可能会转向 MinGW 进行 Windows 开发,所以放弃 MSVC 并不是完全不可能的(如果这被证明是太大的不便) .
【问题讨论】:
-
非常请您尝试解释这些警告!
-
这是stackoverflow.com/q/5661738/417197的副本(或类似)吗?
标签: c++ visual-c++ dll templates