【发布时间】:2012-02-03 15:08:45
【问题描述】:
我正在使用 code::Blocks 开发 oop c++。
这是我在 oop 中的第一步,因为我用 C 语言为微处理器编程。
我在链接 dll 时遇到问题。
我的 dll 项目主要是:
#include "main.h"
#include "xclass.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
这是标题:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#include "xclass.h"
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT SomeFunction(const LPCSTR sometext);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
如您所见的基本内容。
问题是我将 xclass 类包含在 main 中:
#include "xclass.h"
xclass::xclass()
{
//ctor
}
xclass::~xclass()
{
//dtor
}
和标题
#ifndef XCLASS_H
#define XCLASS_H
class xclass
{
public:
xclass();
virtual ~xclass();
unsigned int GetCounter() { return m_Counter; }
void SetCounter(unsigned int val) { m_Counter = val; }
protected:
private:
unsigned int m_Counter;
};
#endif // XCLASS_H
我能够在其他项目中链接和使用该 dll。 A 甚至可以使用 DLL 中的函数SomeFunction("teste x"); 但我无法访问我们的类:
#include <iostream>
#include "main.h"
//#include "../cvWrapper/main.h"
using namespace std;
int main()
{
xclass ClassInDll;// not working
SomeFunction("teste x"); //using the function in dll
printf("%d\n", 1);
return 0;
}
构建错误是:
||=== 测试DLL,调试===| obj\Debug\main.o||在函数
main':| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|9|undefined reference toxclass::xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|未定义 参考xclass::~xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference toxclass::~xclass()'| ||=== 构建完成:3 个错误,0 警告 ===|
感谢您的帮助...
【问题讨论】:
标签: c++ oop dll codeblocks