求助:模板link2001 error的问题[visual studio 2008编译,ACE相关]

jmlt1983 2009-06-27 05:55:34
最近想在open diameter库的基础上做个简单的client demo,所以想在windows下编译这个库文件,但是opendiameter用到了部分ACE的接口,我在这里遇到的问题就是ACE_Singleton接口编译过去不,详细错误如下:

1> Creating library ./MyRelease\DiameterParser.lib and object ./MyRelease\DiameterParser.exp
1>aaa_avplist.obj : error LNK2001: unresolved external symbol "public: static class DiameterAvpTypeList_S * __cdecl ACE_Singleton<class DiameterAvpTypeList_S,class ACE_Recursive_Thread_Mutex>::instance(void)" (?instance@?$ACE_Singleton@VDiameterAvpTypeList_S@@VACE_Recursive_Thread_Mutex@@@@SAPAVDiameterAvpTypeList_S@@XZ)
1>aaa_error_status.obj : error LNK2001: unresolved external symbol "public: static class AAAMemoryManager * __cdecl ACE_Singleton<class AAAMemoryManager,class ACE_Recursive_Thread_Mutex>::instance(void)" (?instance@?$ACE_Singleton@VAAAMemoryManager@@VACE_Recursive_Thread_Mutex@@@@SAPAVAAAMemoryManager@@XZ)
1>./MyRelease\DiameterParser.dll : fatal error LNK1120: 2 unresolved externals

我贴出部分ACE_Singleton的代码下来:
/**
* @class ACE_Singleton
*
* @brief A Singleton Adapter uses the Adapter pattern to turn ordinary
* classes into Singletons optimized with the Double-Checked
* Locking optimization pattern.
*
* This implementation is a slight variation on the GoF
* Singleton pattern. In particular, a single
* <ACE_Singleton<TYPE, ACE_LOCK> > instance is allocated here,
* not a <TYPE> instance. The reason for this is to allow
* registration with the ACE_Object_Manager, so that the
* Singleton can be cleaned up when the process exits. For this
* scheme to work, a (static) cleanup() function must be
* provided. ACE_Singleton provides one so that TYPE doesn't
* need to.
* If you want to make sure that only the singleton instance of
* <T> is created, and that users cannot create their own
* instances of <T>, do the following to class <T>:
* (a) Make the constructor of <T> private (or protected)
* (b) Make Singleton a friend of <T>
* Here is an example:
* @verbatim
* class foo
* {
* friend class ACE_Singleton<foo, ACE_Null_Mutex>;
* private:
* foo () { cout << "foo constructed" << endl; }
* ~foo () { cout << "foo destroyed" << endl; }
* };
* typedef ACE_Singleton<foo, ACE_Null_Mutex> FOO;
* @endverbatim
*
* @note The best types to use for ACE_LOCK are
* ACE_Recursive_Thread_Mutex and ACE_Null_Mutex.
* ACE_Recursive_Thread_Mutex should be used in multi-threaded
* programs in which it is possible for more than one thread to
* access the <ACE_Singleton<TYPE, ACE_LOCK>> instance.
* ACE_Null_Mutex can be used otherwise. The reason that these
* types of locks are best has to do with their allocation by
* the ACE_Object_Manager. Single ACE_Recursive_Thread_Mutex
* and ACE_Null_Mutex instances are used for all ACE_Singleton
* instantiations. However, other types of locks are allocated
* per ACE_Singleton instantiation.
*/
template <class TYPE, class ACE_LOCK>
class ACE_Singleton : public ACE_Cleanup
{
public:
/// Global access point to the Singleton.
static TYPE *instance (void);

/// Cleanup method, used by @c ace_cleanup_destroyer to destroy the
/// ACE_Singleton.
virtual void cleanup (void *param = 0);

/// Explicitly delete the Singleton instance.
static void close (void);

/// Dump the state of the object.
static void dump (void);

protected:
/// Default constructor.
ACE_Singleton (void);

/// Contained instance.
TYPE instance_;

#if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
/// Pointer to the Singleton (ACE_Cleanup) instance.
static ACE_Singleton<TYPE, ACE_LOCK> *singleton_;
#endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */

/// Get pointer to the Singleton instance.
static ACE_Singleton<TYPE, ACE_LOCK> *&instance_i (void);
};

在opendiameter中用到的部分:
class DiameterAvpList_S :
public AAAAvpList
{
friend class ACE_Singleton<DiameterAvpList_S, ACE_Recursive_Thread_Mutex>;

private:
DiameterAvpList_S();
virtual ~DiameterAvpList_S();
};

typedef ACE_Singleton<DiameterAvpList_S, ACE_Recursive_Thread_Mutex> DiameterAvpList;

我对这个问题的感觉:(不知道对不对,大家都提提自己的看法)
从error LNK2001: unresolved external symbol "public: static 应该是instence()这个function的问题....
...全文
228 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jmlt1983 2009-06-27
  • 打赏
  • 举报
回复
谢谢大家的支持,最终还是找出原因了...

问题并非出在Template模版上,而是出在DLL接口的Export和Import上
我编译的是opendiameter的libdiameter,是一个提供diameter协议库的DLL,其中出错的两个link2001 error,都是要向外提供DLL 接口的,但是在opendiameter的code中默认这样个接口都是Import,这样,编译的过程中,就需要从外部链接这样个接口的实现,这是不可能的

所以最终问题出在[接口的方向上了,低级错误]
#if defined (WIN32)
# if defined (AAA_PARSER_EXPORTS)
# define AAA_PARSER_EXPORT ACE_Proper_Export_Flag
# define AAA_PARSER_EXPORT_ONLY ACE_Proper_Export_Flag
# define AAA_PARSER_SINGLETON_DECLARATION(T) ACE_EXPORT_SINGLETON_DECLARATION (T)
# define AAA_PARSER_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) ACE_EXPORT_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK)
# else
# define AAA_PARSER_EXPORT ACE_Proper_Import_Flag
# define AAA_PARSER_EXPORT_ONLY
# define AAA_PARSER_SINGLETON_DECLARATION(T) ACE_IMPORT_SINGLETON_DECLARATION (T)
# define AAA_PARSER_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) ACE_IMPORT_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK)
# endif /* ! AAA_PARSER_EXPORTS */
#else

因为是编译DLL,所以需要预先定义#define AAA_PARSER_EXPORTS 使这两个接口向外提供funciton.
yang_e_2009 2009-06-27
  • 打赏
  • 举报
回复
汗~ 我lib都不会写[Quote=引用 4 楼 jmlt1983 的回复:]
to yang_e_2009 :
function 的确是定义在cpp文件中..但是情况是这样的,这个类模版的定义是在ACE.lib里面,而且在编译ACE.lib也成功了,只是opendiameter调用ACE的ACE_Singleton模版类的时候链接错误....
[/Quote]
KataDoc360 2009-06-27
  • 打赏
  • 举报
回复
link2001 error,检查DLL接口是否实现
jmlt1983 2009-06-27
  • 打赏
  • 举报
回复
to yang_e_2009 :

function 的确是定义在cpp文件中..但是情况是这样的,这个类模版的定义是在ACE.lib里面,而且在编译ACE.lib也成功了,只是opendiameter调用ACE的ACE_Singleton模版类的时候链接错误....


yang_e_2009 2009-06-27
  • 打赏
  • 举报
回复
刚刚试了下,lz大概是把模板类的成员函数定义在.cpp里了吧,
这样的错误正好也是"error LNK2019: 无法解析的外部符号 "public: int __thiscall C<int>::fun(void)" (?fun@?$C@H@@QAEHXZ)

template <typename T> class C
{
T m;
public:
T fun();//成员函数定义在cpp中对于调用不是可见的,定义和调用在同一cpp例外
};
template <typename T> T C<T>::fun(){ return m; } //正确
/******other.cpp**/
template <typename T> T C<T>::fun(){ return m; } //错误
yang_e_2009 2009-06-27
  • 打赏
  • 举报
回复
成员函数的定义在哪里, .h? 还是.cpp?
kondykuang 2009-06-27
  • 打赏
  • 举报
回复
调用约定 问题?

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧