simplest_dll 最简dll的创建与隐式调用(显式调用太麻烦,个人不建议使用)

首先需要有个头文件,名字随便写  假设test.h

//test.h
#ifndef _TEST_H
#define _TEST_H


#ifdef TEST_EXPORTS  //通过宏定义控制是输入还是输出
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

TEST_API int find_max(int,int); //函数声明

#endif

然后要有一个和头文件对应的cpp文件,test.cpp

#define TEST_EXPORTS

#include "stdafx.h"
#include "test.h"

int find_max(int a,int b)
{
    return a>b?a:b;
}

至于dllmain.cpp,可以暂时不去管它。按F7编译,由于函数没有main()函数,不能执行,只能编译。

将生成的simplest_dll.lib和simplest_dll.dll以及test.h文件拷贝到需要调用该dll文件的工程目录下。

在测试工程中包含头文件test.h,并且使用隐式调用的方式实现dll内的函数的调用。

具体代码如下:

// TEST_simplest_dll.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "test.h"
#include <iostream>
using namespace std;

//隐式调用dll文件
#pragma comment(lib,"simplest_dll.lib")  //也可在 属性->配置属性->链接器->输入->附加依赖项 中进行添加*.lib文件
int _tmain(int argc, _TCHAR* argv[])
{
    int a=10;
    int b=100;

    int d=find_max(a,b);
    cout<<"使用隐式调用的结果:"<<d<<endl;
    return 0;
}

这样子就基本完成了一个简单的dll的创建和测试使用。

原文地址:https://www.cnblogs.com/audi-car/p/4437744.html