lib 库 dll 库总结

 https://baike.baidu.com/item/lib/2395664?fr=aladdin

 LIB有两种

一种是静态库,比如C-Runtime库,这种LIB中有函数的实现代码,一般用在静态连编上, 它是将LIB中的代码加入目标模块(EXE或者DLL)文件中,所以链接好了之后,LIB文件就没有用了。

一种LIB是和DLL配合使用的,里面没有代码,代码在DLL中,这种LIB是用在静态调用DLL上的,
所以起的作用也是链接作用,链接完成了,LIB也没用了。至于动态调用DLL的话,根本用不上LIB文件。
目标模块(EXE或者DLL)文件生成之后,就用不着LIB文件了。

 * x86 只能加载 x86 平台下生成 dll
   x64 只能加载 x64 平台下生成 dll

 * 方法1: 通过工程配置加载lib库
    1) 添加头文件(.h)目录:工程 / 属性 / 配置属性 / (c/c++) / 常规 / 附加包含目录: 输入头文件存放目录

    如果该步骤设置错误或未设置。将提示如下错误:
    "fatal error C1083: 无法打开包括文件:"a.h": No such file or directory"


    2) 添加 lib 静态库路径:工程 / 属性 / 配置属性 / 链接器 / 常规 / 附加库目录: 输入 lib 库目录

    如果该步骤设置错误或未设置。将提示如下错误:
    "LINK : fatal error LNK1104: 无法打开文件"a.lib""


    3) 添加 lib 库名:工程 / 属性 / 配置属性 / 链接器 / 输入 / 附加依赖项:输入 lib 库名。

 * 方法2: 用编译语句加载lib库

 #ifdef _DEBUG
 #pragma comment(lib,"..\\debug\\MyDllDemo.lib")
 #else
 #pragma comment(lib,"..\\release\\MyDllDemo.lib")
 #endif

 * 方法3: 直接添加库到工程中,就像添加 .h 和 .cpp 文件一样


 * https://baike.baidu.com/item/.dll/2133451?fr=aladdin
 * .dll,动态链接库英文为DLL,是Dynamic Link Library的缩写。DLL是一个包含可由多个程序,同时使用的代码和数据的库。

    方法1:通过 lib 文件加载 dll
    方法2: 通过 loadlibrary 函数动态加载 dll

1 创建工程 CppDllDemo

CppDllDemo.cpp 代码如下:

#include <iostream>
#include "windows.h"
#include "ICalculate.h"  // 与下文中 ICalculate.h 相同

using namespace std;

// 绝对路径
//#pragma comment(lib,"D:\\Work\\SDK6.3.1\\TestDemo\\Demo_21\\Debug\\MyDllDemo.lib")

// 相对路径
//#pragma comment(lib,"..\\Debug\\MyDllDemo.lib")

int main()
{
    std::cout << "Hello World!\n";

#if 1
	ICalculate* cal = GetMyCalculate();

	int nRet1 = cal->add(6, 3);
	std::cout << "add: " << nRet1 << endl;

	int nRet2 = cal->subtract(6, 3);
	std::cout << "subtract: " << nRet2 << endl;

	int nRet3 = cal->multiply(6, 3);
	std::cout << "multiply: " << nRet3 << endl;

	int nRet4 = cal->divide(6, 3);
	std::cout << "divide: " << nRet4 << endl;
#else
	typedef int(*pAdd)(int a, int b);

	HINSTANCE  HDll = LoadLibrary(L"MyDllDemo.dll");
	pAdd pAddCall = (pAdd)GetProcAddress(HDll, "addTest");

	int m = pAddCall(3, 6);
#endif

	system("pause");
}

2 创建 dll 工程 MyDllDemo

1)  添加类 MyCalculate

MyCalculate.h 代码如下:

#pragma once

#include "ICalculate.h"

class MyCalculate: public ICalculate
{
public:
	int add(int x, int y);
	int subtract(int x, int y);
	int multiply(int x, int y);
	int divide(int x, int y);
};


MyCalculate.cpp 代码如下:

#include "pch.h"
#include "MyCalculate.h"

#include <iostream>

using namespace std;

int MyCalculate::add(int x, int y)
{
	return x + y;
}

int MyCalculate::subtract(int x, int y)
{
	return x - y;
}

int MyCalculate::multiply(int x, int y)
{
	return x * y;
}

int MyCalculate::divide(int x, int y)
{
	return x / y;
}


2) 添加类 ICalculate

ICalculate.h 代码如下:

#pragma once

#define __DLL_EXPORTS__

#ifdef  __DLL_EXPORTS__
#define DLLAPI  __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif

class ICalculate
{
public:
	virtual int add(int x, int y) = 0;
	virtual int subtract(int x, int y) = 0;
	virtual int multiply(int x, int y) = 0;
	virtual int divide(int x, int y) = 0;
};

extern "C" DLLAPI ICalculate *GetMyCalculate();

extern"C" _declspec(dllexport) int addTest(int a, int b);

ICalculate.cpp 代码如下:

#include "pch.h"
#include "MyCalculate.h"

extern "C" DLLAPI ICalculate *GetMyCalculate()
{
	return new MyCalculate();
}

int addTest(int a, int b)
{
	return a + b;
}

项目工程

https://download.csdn.net/download/youqingyike/21899306

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值