简单的 DLL 给我奇怪的编译错误
我正在创建我的第一个 DLL。我只有一个单例类 &我将在 DLL 中创建一个 LRESULT CALLBACK 函数导入到我的一个项目中。我的 MSVC++ 项目架构由 DLLMain.cpp 文件(未更改)、定义单例类和单例类的头文件组成。 LRESULT 函数 &实现 LRESULT 函数的 cpp 文件。
我的问题:该项目未编译。我有两个编译错误,我不明白到底是什么错误&如何修复它。
1>c:\users\testcreatedll\dlltest.h(15): 错误 C2059: 语法错误: '__declspec(dllexport)'
1>c:\users\testcreatedll\dlltest.h(39): 错误 C2065: 'TestWndProc': 未声明的标识符
我的头文件:
#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);
}
}
I am creating my 1st DLL. I have just a singleton class & a LRESULT CALLBACK function that I will create in the DLL & import into one of my projects. My MSVC++ project architecture consists of the DLLMain.cpp file(unaltered), a header file that defines the singleton class & LRESULT function & a cpp file that implements the LRESULT function.
My Problem: the project is not compiling. I have 2 compile errors that I dont understand whats exactly wrong & how to fix it.
1>c:\users\testcreatedll\dlltest.h(15): error C2059: syntax error : '__declspec(dllexport)'
1>c:\users\testcreatedll\dlltest.h(39): error C2065: 'TestWndProc' : undeclared identifier
My header file:
#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
My cpp file:
#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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++ 编译器对于声明调用约定和存储类信息的顺序可能非常挑剔(使用
__declspec
导出可见)。 AFAIK,VC++ 需要调用约定出现在存储类之后。例如:另一方面,C++ Builder 2007 和 MinGW-GCC-4.5.2 并不关心这一点——两种形式都被接受。
C++ compilers can be very picky about the order in which you declare the calling convention and storage-class information(export visibily with
__declspec
). AFAIK, VC++ needs the calling convention to appear after the storage-class. For example:C++ Builder 2007 and MinGW-GCC-4.5.2, on the other-hand, doesn't care about this -- both forms are accepted.