2011-08-13 41 views
1

我試圖創建我的第一個簡單的DLL。我有一個類(這是一個單例類)&一個窗口過程函數,我聲明在DLL &想要在我的項目稍後導入。我的IDE是Microsoft Visual C++ 2010 &我的項目是一個Win32 DLL一個&我已經使用MSVC++默認DLL模板(您知道它創建項目時如何創建所有默認文件)。我的第一個DLL有很多編譯錯誤我不明白

但我得到這些編譯錯誤,我不明白什麼是錯的?

1> C:\用戶\ soribo \保管箱\ C++編程\的Visual C++編程\ testcreatedll \ testcreatedll \ dlltest.h(15):錯誤C2059:語法錯誤: '__declspec(dllimport的)'
1> c:\ users \ soribo \ dropbox \ C++ programming \ visual C++ programming \ testcreatedll \ testcreatedll \ dlltest.h(39):error C2065:'TestWndProc':未聲明的標識符
1> c:\ users \ soribo \ dropbox \ C++ programming \ visual C++ programming \ testcreatedll \ testcreated \ dlltest.cpp(7):warning C4273:'testStaticVar':dll聯動不一致\ testcreatedll \ dlltest.h(21):見前言(7):error C2491:'public:static bool MyTest :: TestClass :: testStaticVar'的定義1> c:\ users \ soribo \ dropbox \ C++ programming \ visual C++ programming \ testcreatedll \ testcreatedll \ dlltest.cpp 'MyTest :: TestClass :: testStaticVar':不允許定義dllimport靜態數據成員
1> c:\ users \ soribo \ dropbox \ C++ programming \ visual C++ programming \ testcreatedll \ testcreatedll \ dlltest.cpp(8):warning C4273:'instance':不一致的dll鏈接
1> c:\ users \ soribo \ dropbox \ C++ programming \ visual C++ programming \ testcreatedll \ testcreatedll \ dlltest.h(35):請參閱'private:static MyTest: :TestClass * MyTest :: TestClass :: instance'
1> c:\ users \ soribo \ dropbox \ C++ programming \ visual C++ programming \ testcreatedll \ testcreatedll \ dlltest.cpp(8 ):錯誤C2491: 'MyTest的::識別TestClass ::實例':dllimport的靜態數據成員的定義不允許

我簡單的頭文件:

#ifndef DLLTEST_H 
#define DLLTEST_H 

#include <windows.h> 

// This is from a tutorial I am following 
#ifdef _CLASSINDLL 
#define CLASSINDLL_CLASS_DECL __declspec(dllexport) 
#else 
#define CLASSINDLL_CLASS_DECL __declspec(dllimport) 
#endif 

namespace MyTest 
{ 
    LRESULT CALLBACK CLASSINDLL_CLASS_DECL TestWndProc(HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam); 

    class CLASSINDLL_CLASS_DECL TestClass 
    { 
     // Singleton class 
     public: 
      static bool testStaticVar; 

      static TestClass* getInstance() 
      { 
       if (instance == NULL) { instance = new TestClass(); } 
       return instance; 
      } 

      void add() 
      { 
       myMember++; 
      } 

     private: 
      static TestClass* instance; 
      WNDPROC myProc; 
      int myMember; 

      TestClass() : myMember(0) { myProc = (WNDPROC)&TestWndProc; } 
      ~TestClass()    {} 

    }; 
} 

#endif // DLLTEST_H 

我簡單的cpp文件:

#include "stdafx.h" 
#include "DLLTest.h" 

namespace MyTest 
{ 
    // Create/Initialise? Class Static variables 
    bool TestClass::testStaticVar = false; 
    TestClass* TestClass::instance = NULL; 

    LRESULT CALLBACK TestWndProc(HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam) 
    { 
     switch (msg) 
     { 
      case WM_CREATE: 
      { 

      } 
      break; 
      default: 
      break; 
     } 

     return DefWindowProc(hwnd, msg, wParam, lParam); 
    } 

} 

回答

0

我想你錯過了_CLASSINDLL預處理器的定義。將其添加到項目 - >屬性 - > C/C++ - >預處理器 - >預處理器定義中。

+0

謝謝,刪除了大量的編譯錯誤,但仍然出現前兩個錯誤。它與靜態類變量有關。也許我不能在dll中使用靜態變量? – user593747

+0

什麼錯誤即?前兩個錯誤與類定義相關,而不是靜態變量。 –

相關問題