在VS中创建和使用DLL动态链接库

VS中使用动态链接库较GCC略微复杂

  • 在VS中新建工程时建立dll工程
  • 在dll工程的头文件中加入如下宏
#ifdef DLL_IMPLEMENT_
#define DLL_APL __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
  • __declspec(dllimport)和__declspec(dllexport)声明了某一对象的导入/导出属性

  • 在dll的实现文件头部使用宏将其声明为dll内部的实现
#define DLL_IMPLEMENT_
  • 如使用dll的工程和dll工程同属一个解决方案,在解决方案的属性中设置好工程的依赖关系,在使用dll的工程的属性中设置好头文件和库文件的路径,即可直接编译运行,需要注意的是,VS编译使用dll的程序时,依然需要该dll对应的lib文件,和gcc直接可以连接动态库有所不同
  • 可以将一个dll的相关内容包含在一个命名空间里,由于命名空间的定义可以不连续,这样也不会对实现和接口的分离造成什么麻烦
程序例子

DLL头文件: DLLtest.h

#ifndef _TEST_H_
#define _TEST_H_

#ifdef DLL_IMPLEMENT_
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

namespace dll
{
    int DLL_API add(const int &a, const int &b);
    int DLL_API sub(const int &a, const int &b);
    int DLL_API mul(const int &a, const int &b);
    int DLL_API div(const int &a, const int &b);
}

#endif

DLL实现文件:DLLtest.cpp

#define DLL_IMPLEMENT_
#include "test.h"

namespace dll
{
    int add(const int &a, const int &b)
    {
        return a + b;
    }

    int sub(const int &a, const int &b)
    {
        return a - b;
    }

    int mul(const int &a, const int &b)
    {
        return a * b;
    }

    int div(const int &a, const int &b)
    {
        return a / b;
    }
}

应用程序:entry.cpp

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

#pragma comment(lib, "DLLtest.lib")

using namespace std;
using namespace dll;

int main()
{
    int a, b;
    char c;
    while( cin >> a >> c >> b )
    {
        switch( c )
        {
        case '+':
            cout << add(a, b) << endl;
            break;
        case '-':
            cout << sub(a, b) << endl;
            break;
        case '*':
            cout << mul(a, b) << endl;
            break;
        case '/':
            cout << dll:: div(a, b) << endl;
            break;
        default:
            cout << '\"' << a << c << b << '\"' << "isn't a valid expression." << endl;
        }
    }
}

Written with StackEdit.

转载于:https://www.cnblogs.com/current/p/3989214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值