我正在尝试创建我的第一个简单DLL。我有一个类(这是一个单例类)&我在DLL&中声明的Window Procedure函数。我想稍后在我的项目中导入。我的IDE是Microsoft Visual C ++ 2010&我的项目是Win32 DLL one&我使用了MSVC ++默认的DLL模板(你知道在我创建项目时它是如何创建所有默认文件的。)
但是我收到这些编译错误,我不明白这是什么问题?
1> c:\ users \ soribo \ dropbox \ c ++ programming \ visual c ++ programming \ testcreatedll \ testcreatedll \ dlltest.h(15):error 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 \ testcreatedll \ dlltest.cpp(7):警告C4273:'testStaticVar':不一致的dll链接
1 GT; c:\ users \ soribo \ dropbox \ c ++ programming \ visual c ++ programming \ testcreatedll \ testcreatedll \ dlltest.h(21):参见先前的'public:static bool MyTest :: TestClass :: testStaticVar'的定义 1> c:\ users \ soribo \ dropbox \ c ++ programming \ visual c ++ programming \ testcreatedll \ testcreatedll \ dlltest.cpp(7):error C2491:'MyTest :: TestClass :: testStaticVar':不允许定义dllimport静态数据成员
1> c:\ users \ soribo \ dropbox \ c ++ programming \ visual c ++ programming \ testcreatedll \ testcreatedll \ dlltest.cpp(8):警告C4273:'instance':不一致的dll链接
1 GT; 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):error C2491:'MyTest :: TestClass :: instance':不允许定义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 :(得分:0)
我认为你缺少_CLASSINDLL预处理器定义。在项目中添加它 - &gt;属性 - &gt; C / C ++ - &gt;预处理器 - &gt;预处理器定义。