我正在尝试创建我的第一个简单DLL。我有一个类(这是一个单例类)和一个在DLL中声明的窗口过程函数,希望稍后再导入我的项目中。我的IDE是Microsoft Visual C ++ 2010,我的项目是Win32 DLL,我使用了MSVC ++默认DLL模板(您知道我在创建项目时如何创建所有默认文件)。
但是我遇到了这些编译错误,我不明白哪里出了问题?
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.h(15):错误C2059:语法错误:'__declspec(dllimport)'
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.h(39):错误C2065:'TestWndProc':未声明的标识符
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.cpp(7):警告C4273:'testStaticVar':DLL链接不一致
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.h(21):参见'public:static bool MyTest :: TestClass :: testStaticVar'的先前定义
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.cpp(7):错误C2491:'MyTest :: TestClass :: testStaticVar':dllimport静态数据成员的定义不允许
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.cpp(8):警告C4273:'instance':DLL链接不一致
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.h(35):请参见“ private:static MyTest :: TestClass * MyTest :: TestClass :: instance'的先前定义
1> c:\ users \ soribo \ dropbox \ c ++编程\ Visual c ++编程\ testcreatedll \ testcreatedll \ dlltest.cpp(8):错误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);
}
}
最佳答案
我认为您缺少_CLASSINDLL预处理程序定义。将其添加到项目->属性-> C / C ++->预处理程序->预处理程序定义中。
关于c++ - 我的第一个DLL有很多我不理解的编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7049662/