C++动态库创建及使用2

2 篇文章 0 订阅
1 篇文章 0 订阅

对于面向对象程序,经常需要导出类。动态库中类的导出方法和导出函数的方法是一样的。首先创建动态库项目classDll,并在项目中加入头文件classdll.h和源文件classdll.cpp。

classdll.h

#ifndef CLASS_DLL_H
#define CLASS_DLL_H

#ifdef WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

class DLL_EXPORT CDll
{
public:
    CDll();
    ~CDll();
    int& getFromDll();

private:
    int m_nValue;
};
#endif

classdll.cpp

#include "classdll.h"

CDll::CDll()
    :m_nValue(0)
{

}

CDll::~CDll()
{

}

int& CDll::getFromDll()
{
    return m_nValue;
}
接下来,写编写调用程序

dlltest.cpp

#include <iostream>
#include "classdll.h"

int main(int argc, char* argv[])
{
    CDll dll;
    int& nValue = dll.getFromDll();
    std::cout<<nValue<<std::endl;
    nValue = 10;
    int& nValue2 = dll.getFromDll();
    std::cout<<nValue2<<std::endl;
    std::cin.get();
    return 0;
}

程序能够成功编译并正确执行。接下来对第三方库程序作小小的改动,如下

classdll.h

#ifndef CLASS_DLL_H
#define CLASS_DLL_H

#ifdef WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

class DLL_EXPORT CDll
{
public:
    CDll();
    ~CDll();
    int& getFromDll();

    static int m_nValue;
};
#endif

classdll.cpp

#include "classdll.h"

CDll::CDll()
{

}

CDll::~CDll()
{

}

int& CDll::getFromDll()
{
    return m_nValue;
}

int CDll::m_nValue = 10;


替换调用程序中的相关库文件和头文件,并修改调用程序为:

dllTest.cpp

#include <iostream>
#include "classdll.h"

int main(int argc, char* argv[])
{
    CDll dll;
    int& nValue = dll.getFromDll();
    std::cout<<nValue<<std::endl;

    dll.m_nValue = 999;//访问动态库中的成员变量
    int& nValue2 = dll.getFromDll();
    std::cout<<nValue2<<std::endl;
    std::cin.get();
    return 0;
}

编译发现出现错误:

1>Link:
1>     正在创建库 c:\users\administrator\documents\visual studio 2010\Projects\dllTest\Debug\dllTest.lib 和对象 c:\users\administrator\documents\visual studio 2010\Projects\dllTest\Debug\dllTest.exp
1>dlltest.obj : error LNK2001: 无法解析的外部符号 "public: static int CDll::m_nValue" (?m_nValue@CDll@@2HA)
1>c:\users\administrator\documents\visual studio 2010\Projects\dllTest\Debug\dllTest.exe : fatal error LNK1120: 1 个无法解析的外部命令


原来引用动态库中的成员变量是需要显示说明此变量是外部库文件中申明的。其方法是在类定义前加上__declspec(import),所以可以通过如下修改解决此问题:

classdll.h

#ifndef CLASS_DLL_H
#define CLASS_DLL_H

#ifndef EXTERN_VALUE
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

class DLL_EXPORT CDll
{
public:
    CDll();
    ~CDll();
    int& getFromDll();

    static int m_nValue;
};
#endif

dlltest.cpp

#include <iostream>
#define EXTERN_VALUE
#include "classdll.h"

int main(int argc, char* argv[])
{
    CDll dll;
    int& nValue = dll.getFromDll();
    std::cout<<nValue<<std::endl;

    dll.m_nValue = 999;//访问动态库中的成员变量
    int& nValue2 = dll.getFromDll();
    std::cout<<nValue2<<std::endl;
    std::cin.get();
    return 0;
}

这样程序就能成功执行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值