For production and use of dynamic link libraries in vs2017 IDE

First, make a dynamic library dll

(1) create a dynamic library project Dll-1, as shown in Figure

(2) a new header file Dll-1.h

// Dll-1.h
#ifdef Dll_1_API
#else
#define Dll_1_API _declspec(dllimport)
#endif // Dll_1_API

Dll_1_API int add(int a, int b);
Dll_1_API int subtract(int a, int b);

 

Wherein, _declspec (dllimport) in order to add function declarations and substract function derived from the dynamic dll library may also be declared by the class _declspec (dllimport) or class member functions derived from the dynamic dll library.

(3) New Dll-1.cpp implement the functions of the function

// Dll-1.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#define Dll_1_API _declspec(dllexport)
#include "Dll-1.h"
int add(int a, int b)
{
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

 

(4)右键项目,点击生成解决方案,同时会生成lib和dll文件。

二、新建调用动态库的测试项目Dll-1-test

(1)在项目中加入动态库的头文件

// Dll-1.h
#ifdef Dll_1_API
#else
#define Dll_1_API _declspec(dllimport)
#endif // Dll_1_API

Dll_1_API int add(int a, int b);
Dll_1_API int subtract(int a, int b);

 

(2)在源文件中调用dll动态库的方法

#include <stdlib.h>
#include <iostream>
#include "Dll-1.h"
using namespace std;

int main() {
    int x = 3; 
    int y = 6;
    int m = add(x, y);
    int n = subtract(x, y);
    cout << "m: " << m << endl;
    cout << "n: " << n << endl;
    system("pause");
    return 0;
}

 

(3)进行项目属性配置

在测试项目工程中配置lib文件的引入路径,同时将lib文件,dll文件复制到测试项目中。点击链接生成解决方案。

(4)运行测试项目

结果如下

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/madFish/p/11918248.html