2012-05-23 51 views
0

我想在VS2010中構建別人的代碼。它在VS2005中構建得很好,但我需要升級它。模板宏的鏈接器錯誤:無法解析的外部符號

他們定義的宏作爲在頭如下:

#include <boost/scoped_ptr.hpp> 
#include <boost/utility.hpp> 
#include <boost/thread/once.hpp> 

#define DEFINE_SINGLETON(name) boost::scoped_ptr<name> Singleton<name>::_instance 

template<class T> class Singleton : private boost::noncopyable 
{ 
    public: 
     static T& instance() 
     { 
      boost::call_once(init, flag);   
      return *_instance; 
     } 

     static void init() 
     { 
      _instance.reset(new T()); 
     } 

    protected: 
     Singleton() {} 
     ~Singleton() {} 

    private: 
     static boost::scoped_ptr<T> _instance; 
     static boost::once_flag flag; 
}; 

template<class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT; 

我已經成功地得到現在的代碼來構建,但我獲得了大量的連接錯誤的這個宏:

project1.lib(file1.obj) : error LNK2001: unresolved external symbol "private: static class boost::scoped_ptr<class ClassABC> Singleton<class ClassABC>::_instance" ([email protected][email protected]@@@@[email protected]@@@[email protected]@A) 

宏的一個例子是使用(源文件):

#include "singleton.h" 
DEFINE_SINGLETON(ClassABC); 

我很新,來推動和也templat es(對不起),所以我無法理解爲什麼當VS2005中的所有鏈接都很好的時候出現這些錯誤。它值得一提的,是爲了做升級,我們也必須提升我們的加速版本,所以這可能是一個因素 - 一些檢查已經完成:

  • 頭文件包含在源文件
  • 升壓目錄添加到include目錄(下VC++迪爾斯inproperty頁)
  • 添加上去的lib目錄到鏈接器 - >常規 - >附加庫的依賴

有關信息,我在編譯多線程調試DLL Win32的。

我花了大部分時間使用Google搜索無濟於事,所以非常感謝任何幫助!

非常感謝:)

+0

你使用'DEFINE_SINGLETON'宏地方? –

+0

對不起,現在包含使用的宏的例子 – namford

回答

0

我沒有看到你所用的發佈代碼的宏的任何地方。

您需要使用它:

//add this line after BOOST_ONCE_INIT or before it, or wherever you see fit. 
template<class T> DEFINE_SINGLETON(T); 

個人而言,我更喜歡這樣的:

template<class T> boost::scoped_ptr<T> Singleton<T>::_instance; 
+0

對不起,現在包含使用的宏的例子 – namford

+0

您忘記使用'模板'部分。看我的代碼。 – Nawaz

+0

謝謝,但代碼是這樣的VS2005和宏調用是這樣的(沒有類型)遍及代碼,並在VS2005中工作得很好......爲什麼VS2010會有所不同? – namford

相關問題