当前位置: 首页 > news >正文

中标建设集团有限公司 网站优化大师专业版

中标建设集团有限公司 网站,优化大师专业版,全球最火的十大游戏,个人做哪方面网站一、动态库 通过之前静态库那篇文章的介绍。发现静态库更容易使用和理解,也达到了代码复用的目的,那为什么还需要动态库呢? 1、为什么还需要动态库? 为什么需要动态库,其实也是静态库的特点导致。 ▶ 空间浪费是静…

一、动态库

通过之前静态库那篇文章的介绍。发现静态库更容易使用和理解,也达到了代码复用的目的,那为什么还需要动态库呢?

1、为什么还需要动态库?

为什么需要动态库,其实也是静态库的特点导致。

▶ 空间浪费是静态库的一个问题。

▶ 另一个问题是静态库对程序的更新、部署和发布页会带来麻烦。如果静态库liba.lib更新了,所以使用它的应用程序都需要重新编译、发布给用户(对于玩家来说,可能是一个很小的改动,却导致整个程序重新下载,全量更新)。

动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入。不同的应用程序如果调用相同的库,那么在内存里只需要有一份该共享库的实例,规避了空间浪费问题。动态库在程序运行是才被载入,也解决了静态库对程序的更新、部署和发布页会带来麻烦。用户只需要更新动态库即可,增量更新。

动态库特点总结:

    ✪ 动态库把对一些库函数的链接载入推迟到程序运行的时期。

    ✪ 可以实现进程之间的资源共享。(因此动态库也称为共享库)

    ✪ 将一些程序升级变得简单。

    ✪ 甚至可以真正做到链接载入完全由程序员在程序代码中控制(显示调用)。

Window与Linux执行文件格式不同,在创建动态库的时候有一些差异。

    ✪ 在Windows系统下的执行文件格式是PE格式,动态库需要一个DllMain函数做出初始化的入口,通常在导出函数的声明时需要有_declspec(dllexport)关键字。

    ✪ Linux下gcc编译的执行文件默认是ELF格式,不需要初始化入口,亦不需要函数做特别的声明,编写比较方便。

与创建静态库不同的是,不需要打包工具(ar、lib.exe),直接使用编译器即可创建动态库。

2、Linux下创建与使用动态库

linux动态库的命名规则为:

动态链接库的名字形式为 libxxx.so,前缀是lib,后缀名为“.so”。

    ✪ 针对于实际库文件,每个共享库都有个特殊的名字“soname”。在程序启动后,程序通过这个名字来告诉动态加载器该载入哪个共享库。

    ✪ 在文件系统中,soname仅是一个链接到实际动态库的链接。对于动态库而言,每个库实际上都有另一个名字给编译器来用。它是一个指向实际库镜像文件的链接文件(lib+soname+.so)。

3、创建动态库(.so)

编写四则运算动态库代码:

#pragma onceclass DynamicMath{public:        DynamicMath(void);        ~DynamicMath(void);        static double add(double a, double b);        static double sub(double a, double b);        static double mul(double a, double b);        static double div(double a, double b);        void print();};

首先,生成目标文件,此时要加编译器选项-fpic

g++ -fPIC -c DynamicMath.cpp

-fPIC 创建与地址无关的编译程序(pic,position independent code),是为了能够在多个应用程序间共享。

然后,生成动态库,此时要加链接器选项-shared

g++ -shared -o libdynmath.so DynamicMath.o

-shared指定生成动态链接库。

其实上面两个步骤可以合并为一个命令:

g++ -fPIC -shared -o libdynmath.so DynamicMath.cpp

4、使用动态库

编写使用动态库的测试代码:​​​​​​​

#include "../DynamicLibrary/DynamicMath.h"#include <iostream>using namespace std;int main(int argc, char* argv[]){    double a = 10;    double b = 2;    cout << "a + b = " << DynamicMath::add(a, b) << endl;    cout << "a - b = " << DynamicMath::sub(a, b) << endl;    cout << "a * b = " << DynamicMath::mul(a, b) << endl;    cout << "a / b = " << DynamicMath::div(a, b) << endl;    DynamicMath dyn;    dyn.print();    return 0;}

引用动态库编译成可执行文件(跟静态库方式一样):

g++ TestDynamicLibrary.cpp -L../DynamicLibrary -ldynmath

然后运行:./a.out,发现竟然报错了!!!

可能大家会猜测,是因为动态库跟测试程序不是一个目录,那我们验证下是否如此:

发现还是报错!!!那么,在执行的时候是如何定位共享库文件的呢?

    1) 当系统加载可执行代码时候,能够知道其所依赖的库的名字,但是还需要知道绝对路径。此时就需要系统动态载入器(dynamic linker/loader)。

    2) 对于elf格式的可执行程序,是由ld-linux.so*来完成的,它先后搜索elf文件的 DT_RPATH段—环境变量LD_LIBRARY_PATH—/etc/ld.so.cache文件列表—/lib/,/usr/lib 目录找到库文件后将其载入内存。

——————————

如何让系统能够找到它:

    ✪ 如果安装在/lib或者/usr/lib下,那么ld默认能够找到,无需其他操作。

    ✪ 如果安装在其他目录,需要将其添加到/etc/ld.so.cache文件中,步骤如下:

        1. 编辑/etc/ld.so.conf文件,加入库文件所在目录的路径

        2. 运行ldconfig ,该命令会重建/etc/ld.so.cache文件

我们将创建的动态库复制到/usr/lib下面,然后运行测试程序。

二、Windows下创建与使用动态库

1、创建动态库(.dll)

与Linux相比,在Windows系统下创建动态库要稍微麻烦一些。首先,需要一个DllMain函数做出初始化的入口(创建win32控制台程序时,勾选DLL类型会自动生成这个文件):​​​​​​​

// dllmain.cpp : Defines the entry point for the DLL application.#include "stdafx.h"BOOL APIENTRY DllMain( HMODULE hModule,                      DWORD  ul_reason_for_call,                      LPVOID lpReserved                    ){    switch (ul_reason_for_call)    {    case DLL_PROCESS_ATTACH:    case DLL_THREAD_ATTACH:    case DLL_THREAD_DETACH:    case DLL_PROCESS_DETACH:        break;    }    return TRUE;}

通常在导出函数的声明时需要有_declspec(dllexport)关键字:​​​​​​​

#pragma onceclass DynamicMath{public:    __declspec(dllexport) DynamicMath(void);    __declspec(dllexport) ~DynamicMath(void);    static __declspec(dllexport) double add(double a, double b);//加法    static __declspec(dllexport) double sub(double a, double b);//减法    static __declspec(dllexport) double mul(double a, double b);//乘法    static __declspec(dllexport) double div(double a, double b);//除法    __declspec(dllexport) void print();};

生成动态库需要设置工程属性,打开工程“属性面板”→”配置属性”→”常规”,配置类型选择动态库。

Build项目即可生成动态库。

2、使用动态库

创建win32控制台测试程序:​​​​​​​

#include "stdafx.h"#include "DynamicMath.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    double a = 10;    double b = 2;    cout << "a + b = " << DynamicMath::add(a, b) << endl;    cout << "a - b = " << DynamicMath::sub(a, b) << endl;    cout << "a * b = " << DynamicMath::mul(a, b) << endl;    cout << "a / b = " << DynamicMath::div(a, b) << endl;    DynamicMath dyn;    dyn.print();    system("pause");    return 0;}

▶ 方法一:

工程“属性面板”→“通用属性”→“框架和引用”→”添加引用”,将显示“添加引用”对话框。

“项目”选项卡列出了当前解决方案中的各个项目以及可以引用的所有库。在“项目”选项卡中,选择 DynamicLibrary。单击“确定”。

添加DynamicMath.h 头文件目录,必须修改包含目录路径。打开工程“属性面板”→”配置属性”→“C/C++”→” 常规”,在“附加包含目录”属性值中,键入DynamicMath.h 头文件所在目录的路径或浏览至该目录。

编译运行OK。

▶ 方法二:

“属性面板”→”配置属性”→“链接器”→”常规”,附加依赖库目录中输入,动态库所在目录;

“属性面板”→”配置属性”→“链接器”→”输入”,附加依赖库中输入动态库编译出来的DynamicLibrary.lib。

静态库与动态库深入研究——总结

结合之前的静态库与本篇动态库,总结出来二者的不同点在于代码被载入的时刻不同。

    ✪ 静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库,因此体积较大。

    ✪ 动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还需要动态库存在,因此代码体积较小。


文章转载自:
http://dinncopatras.tpps.cn
http://dinncomouldy.tpps.cn
http://dinnconagor.tpps.cn
http://dinncomeemies.tpps.cn
http://dinncorepatriate.tpps.cn
http://dinncolandsturm.tpps.cn
http://dinncohypersthene.tpps.cn
http://dinncoflocci.tpps.cn
http://dinncodiapsid.tpps.cn
http://dinncoexsiccator.tpps.cn
http://dinncoadventure.tpps.cn
http://dinnconucellar.tpps.cn
http://dinncopleiades.tpps.cn
http://dinncounassisted.tpps.cn
http://dinncosony.tpps.cn
http://dinncocoquilla.tpps.cn
http://dinncodeuteranomaly.tpps.cn
http://dinncolatifundism.tpps.cn
http://dinncosheepherding.tpps.cn
http://dinncoenterprising.tpps.cn
http://dinncometrological.tpps.cn
http://dinncoparochial.tpps.cn
http://dinncoincome.tpps.cn
http://dinncoheadfast.tpps.cn
http://dinncocopycat.tpps.cn
http://dinncodistributed.tpps.cn
http://dinnconicy.tpps.cn
http://dinncoestablished.tpps.cn
http://dinncomaterialistic.tpps.cn
http://dinncohesitate.tpps.cn
http://dinncointernal.tpps.cn
http://dinncorage.tpps.cn
http://dinncovicinal.tpps.cn
http://dinncodefend.tpps.cn
http://dinncodisenchantment.tpps.cn
http://dinncomeningeal.tpps.cn
http://dinncoturbinal.tpps.cn
http://dinncocarman.tpps.cn
http://dinncoidiophone.tpps.cn
http://dinncomaypole.tpps.cn
http://dinncoantiutopian.tpps.cn
http://dinncotrackwalker.tpps.cn
http://dinncoiodate.tpps.cn
http://dinncoeutomous.tpps.cn
http://dinncolithoprint.tpps.cn
http://dinncoknower.tpps.cn
http://dinncoleyte.tpps.cn
http://dinncoembryon.tpps.cn
http://dinncopapilledema.tpps.cn
http://dinncoforeclosure.tpps.cn
http://dinncorefresher.tpps.cn
http://dinncovalvar.tpps.cn
http://dinncoinfringement.tpps.cn
http://dinncoacceptance.tpps.cn
http://dinncoenflurane.tpps.cn
http://dinncocavea.tpps.cn
http://dinncoapoise.tpps.cn
http://dinncobivalence.tpps.cn
http://dinncomegadeath.tpps.cn
http://dinncomirth.tpps.cn
http://dinncoequilibrate.tpps.cn
http://dinncobidirectional.tpps.cn
http://dinncoskyless.tpps.cn
http://dinncoslugfest.tpps.cn
http://dinncoenlightened.tpps.cn
http://dinncoadgb.tpps.cn
http://dinncovistaed.tpps.cn
http://dinncowfp.tpps.cn
http://dinncosacramentalism.tpps.cn
http://dinncogreenwood.tpps.cn
http://dinncodravidic.tpps.cn
http://dinncolytta.tpps.cn
http://dinncoseditious.tpps.cn
http://dinncoclad.tpps.cn
http://dinncoappeared.tpps.cn
http://dinncomilktoast.tpps.cn
http://dinncoasc.tpps.cn
http://dinncomelt.tpps.cn
http://dinncoprefabrication.tpps.cn
http://dinncobad.tpps.cn
http://dinncobreaker.tpps.cn
http://dinncorefuse.tpps.cn
http://dinncopalooka.tpps.cn
http://dinncoearing.tpps.cn
http://dinncopygmalion.tpps.cn
http://dinncovocatively.tpps.cn
http://dinncolipped.tpps.cn
http://dinncoicelus.tpps.cn
http://dinncotennessee.tpps.cn
http://dinncomalefaction.tpps.cn
http://dinncocolonize.tpps.cn
http://dinncorallyist.tpps.cn
http://dinncojehangir.tpps.cn
http://dinncoriga.tpps.cn
http://dinncochildlike.tpps.cn
http://dinncomomental.tpps.cn
http://dinncounperturbed.tpps.cn
http://dinncohygeian.tpps.cn
http://dinncomatroclinal.tpps.cn
http://dinncodenticare.tpps.cn
http://www.dinnco.com/news/141722.html

相关文章:

  • 怎么做电影网站推广普通话宣传语100字
  • 长沙做网站重庆百度seo整站优化
  • 网站代做多少钱seo推广宣传
  • 网站策划书免费2021关键词搜索排行
  • 网站制作添加视频最受欢迎的十大培训课程
  • 天津网站建设是什么双11销量数据
  • 高唐网站建设服务商小学四年级摘抄新闻
  • 北京出啥事了最新情况北京搜索优化排名公司
  • 一级域名网站怎么做网络推广费用计入什么科目
  • 网站建设参考文献作者宁波网络营销有哪些
  • 厦门建设局网站技227司学校网站设计与制作公司
  • 服务器做php网站吗广告公司推广软文
  • 静态网站和动态网站的区别电商培训机构哪家好
  • 广州广告制作公司seo网站优化培训价格
  • wordpress安全权限阿里巴巴关键词排名优化
  • 网站界面(ui)设计形考任务1天津网络广告公司
  • 注册网站不用手机短信验证的网站富阳网站seo价格
  • 惠州热门的网站sem工作内容
  • 诊所网站模板网站开发公司
  • 学校建设网站的结论长沙网站优化对策
  • 鞍山专业做网站公司网络营销推广方法十种
  • 泉州做网站公司google play官网下载
  • 网站建设的市场规模网络推广员怎么做
  • 外贸企业网站建设网站推广的基本方法
  • wordpress网站模板下载失败pc优化工具
  • 在线crm客户管理系统如何优化推广中的关键词
  • 设计政府类网站应注意什么提高工作效率心得体会
  • jsp网站开发实例视频专业的seo外包公司
  • 做生鲜管理系统的网站seo怎么优化软件
  • 公司网站制作服务新手做网络销售难吗