【问题标题】:Object Oriented Programming c++ dll Code::Blocks面向对象编程 c++ dll Code::Blocks
【发布时间】: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


    【解决方案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;
    };
    

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

    【讨论】:

    • 什么宏? WINAPI? DLL 包括警卫?
    【解决方案2】:

    你也需要导出类:

    class DLL_EXPORT xclass {
      //...
    

    您可能需要稍微重新排列标题 - 例如将#define for DLL_EXPORT 放在可以包含在“main.h”和“xclass.h”中的某个位置。

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

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 2011-04-16
      • 1970-01-01
      • 2014-01-10
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多