gpt4 book ai didi

c++ - 面向对象编程 c++ dll Code::Blocks

转载 作者:行者123 更新时间:2023-11-28 08:10:47 24 4
gpt4 key购买 nike

我正在使用 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__

如您所见的基本内容。

问题是我在 main 中包含了 xclass 类:

#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;
}

构建错误是:

||=== testDLL, Debug ===| obj\Debug\main.o||In function main':|
C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|9|undefined
reference to
xclass::xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference to xclass::~xclass()'|
C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined
reference to
xclass::~xclass()'| ||=== Build finished: 3 errors, 0 warnings ===|

感谢您的帮助...

最佳答案

实际上你应该导出类:

class DLL_EXPORT 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;
};

导出不是纯虚类的类时要小心,因为您可能会遇到一些内存对齐问题。发生这种情况是因为不同编译器中的不同 RTL 版本。而是导出您类的纯虚拟接口(interface)。

class DLL_EXPORT IXClass
{
public:
IXClass();
virtual ~IXClass();
virtual unsigned int GetCounter()=0;
virtual void SetCounter(unsigned int val) =0;
};

同时避免使用宏...祝你好运:)。

关于c++ - 面向对象编程 c++ dll Code::Blocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130828/

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