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

时间:2012-02-03 15:08:45

标签: c++ oop dll codeblocks

我正在使用代码:: 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包含在主:

#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 ||在函数main':| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|9|undefined reference to xclass :: xclass()'|中   C:\ Users \用户SoftVision \桌面\ PrinterCode \ DLL_test \ testDLL \ main.cpp中| 14 |未定义   引用xclass::~xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference to xclass :: ~xclass()'| || ===构建完成:3个错误,0   警告=== |

感谢帮助...

2 个答案:

答案 0 :(得分:1)

实际上你应该导出这个类:

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版本不同。而是导出您的类的纯虚拟接口。

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

还要避免使用宏... 祝你好运:)。

答案 1 :(得分:0)

您也需要导出课程:

class DLL_EXPORT xclass {
  //...

您可能需要稍微重新排列标题 - 例如将#define放在DLL_EXPORT的某处,可以包含在'main.h'和'xclass.h'中。

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL

相关问题