[英]Simple DLL giving me weird compile errors
我正在創建我的第一個 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.
我的問題:項目沒有編譯。 我有 2 個編譯錯誤,我不明白到底是什么錯誤以及如何修復它。
1>c:\users\testcreatedll\dlltest.h(15): 錯誤 C2059: 語法錯誤: '__declspec(dllexport)'
1>c:\users\testcreatedll\dlltest.h(39): 錯誤 C2065: 'TestWndProc': 未聲明的標識符
我的 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);
}
}
C++ 編譯器可能對您聲明調用約定和存儲類信息的順序非常挑剔(使用__declspec
明顯地導出)。 AFAIK,VC++ 需要調用約定出現在存儲類之后。 例如:
namespace MyTest
{
LRESULT CLASSINDLL_CLASS_DECL CALLBACK TestWndProc( HWND hwnd, UINT msg, LPARAM lParam, WPARAM wParam );
// ...
}
另一方面,C++ Builder 2007 和 MinGW-GCC-4.5.2 並不關心這一點——forms 都被接受。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.