[英]My first DLL has many compile errors I don't understand
我正在尝试创建我的第一个简单 DLL。 I have a class(that's a singleton class) & a Window Procedure function that I declare in the DLL & want to import in my project later on. My IDE is Microsoft Visual C++ 2010 & my project is a Win32 DLL one & I have used MSVC++ default DLL template(you know how it creates all the default files when I create the project).
但是我收到了这些编译错误,我不明白出了什么问题?
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++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(21): see previous definition of 'public: static bool MyTest::TestClass::testStaticVar'
1>c:\users\soribo\dropbox\c++ 编程\visual c++ 编程\testcreatedll\testcreatedll\dlltest.cpp(7): 错误 C2491: 'MyTest::TestClass::testStaticVar': 定义 dllimport ZA81259CEF8E959C624DF1D456E5D39 不允许成员数据
1>c:\users\soribo\dropbox\c++ 编程\visual c++ 编程\testcreatedll\testcreatedll\dlltest.cpp(8): 警告 C4273: 'instance': 不一致的 dll 链接
1> c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(35): see previous definition of '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 static 数据成员不允许
我的简单 header 文件:
#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++ -> 预处理器 -> 预处理器定义中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.