gpt4 book ai didi

c++ - 我的第一个 DLL 有很多我不明白的编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:48 36 4
gpt4 key购买 nike

我正在尝试创建我的第一个简单 DLL。我有一个类(这是一个单例类)和一个我在 DLL 中声明的窗口过程函数,稍后想在我的项目中导入.我的 IDE 是 Microsoft Visual C++ 2010,我的项目是一个 Win32 DLL,我使用的是 MSVC++ 默认 DLL 模板(你知道它是如何在我创建项目时创建所有默认文件的)。

但我收到这些编译错误,我不明白哪里出了问题?

1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(15): error C2059: syntax error : '__declspec(dllimport)'
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.h(39): error C2065: 'TestWndProc' : undeclared identifier
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(7): warning C4273: 'testStaticVar' : inconsistent dll linkage
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++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(7): error C2491: 'MyTest::TestClass::testStaticVar' : definition of dllimport static data member not allowed
1>c:\users\soribo\dropbox\c++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(8): warning C4273: 'instance' : inconsistent dll linkage
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++ programming\visual c++ programming\testcreatedll\testcreatedll\dlltest.cpp(8): error C2491: 'MyTest::TestClass::instance' : definition of dllimport static data member not allowed

我的简单头文件:

#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 预处理器定义。在 Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions 中添加它。

关于c++ - 我的第一个 DLL 有很多我不明白的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7049662/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com