静态链接库、动态链接库使用方法

 总结一下动态链接库和静态链接库。

首先搞清楚几个概念:

静态库:函数和数据被编译进一个二进制文件,通常扩展名为.lib。在使用静态库的情况下,在编译连接可执行文件时,链接器从库中复制这些函数和数据并把它们和应用程序的其他模块组合起来创建最终的可执行文件(.exe文件)。

动态库:在使用动态库的时候,往往提供两个文件:1.引入库(.lib)和.dll。引入库包含被Dll导出的函数和变量的符号名,Dll包含实际的函数和数据。在编译链接可执行文件时,只需要链接引入库,Dll中的函数代码和数据并不复制到可执行文件中,在运行的时候,再去加载Dll,访问Dll中导出的函数。

静态连接库就是把(lib)文件中用到的函数代码直接链接进目标程序,程序运行的时候不再需要其它的库文件;动态链接就是把调用的函数所在文件模块(Dll)和调用函数在文件中的位置等信息链接进目标程序,程序运行的时候再从Dll中寻找相应函数代码,因此需要相应Dll文件的支持。

Lib:静态链接库和动态链接库都会生成.lib文件,但.lib文件却有着不同的内容。

目标库(Object Libraries)

目标库又叫静态链接库,是扩展名为.LIB的文件,包括了用户程序要用到 的各种函数。它在用户程序进行链接时,“静态链接”到可执行程序文件当中。例如,在VC++中最常使用到的C运行时目标库文件就是LIBC.LIB。在链 接应用程序时常使用所谓“静态链接”的方法,即将各个目标文件(.obj)、运行时函数库(.lib)以及已编译的资源文件(.res)链接到一起,形成 一个可执行文件(.exe)。使用静态链接时,可执行文件需要使用的各种函数和资源都已包含到文件中。这样做的缺点是对于多个程序都使用的相同函数和资源 要重复链接到exe文件中,使程序变大、占用内存增加。  

导入库(Import Libraries)

导入库是一种特殊形式的目标库文件形式。和目标库文件一样,导入库文件的扩展名也是.LIB,也是在用户程序被链接时,被“静态链接”到可执行文件当中。但是不同的是,导入库文件中并不包含有程序代码。相应的,它包含了相关的链接信息,帮助应用程序在可执行文件中建立起正确的对应于动态链接库的重定向表。比如KERNEL32.LIB、USER32.LIB和GDI32.LIB就是我们常用到的导入库,通过它们,我们就可以调用Windows提供的函数了。 如果我们在程序中使用到了Rectangle这个函数,GDI32.LIB就可以告诉链接器,这个函数在GDI32.DLL动态链接库文件中。这样,当用 户程序运行时,它就知道“动态链接”到GDI32.DLL模块中以使用这个函数。其实说白了导入库就是一个索引,一个dll动态链接库的索引表,这是我的 理解。

Dll:

“动态链接”是将一些公用的函数或资源组织成动态链接库文件(.dll),当某个需要使用dll中的函数或资源的程序启动时(准确的说是初始化时),系统将该 dll映射到调用进程的虚拟地址空间、增加该dll的引用计数值,然后当实际使用到该dll时操作系统就将该dll载入内存;如果使用该dll的所有程序 都已结束,则系统将该库从内存中移除。使用同一dll的各个进程在运行时共享dll的代码,但是对于dll中的数据则各有一份拷贝(当然也有在dll中共享数据的方法)。 动态链接库中可以定义两种函数:输出函数和内部函数。输出函数可以被其他模块调用,内部函数只能被动态链接库本身调用。动态链接库也可以输出数据,但这些数据通常只被它自己的函数所使用。

.h文件:头文件,变量及函数声明就在.h文件中。

.pdb文件:用来帮助软件进行调试的,无论是静态链接库、动态链接库的静态调用、动态调用只要有.pdb文件就能进行调试。

extern "C":不发生名字改编,解决C++和C语言之间调用时名字改编的问题,加上之后不改变函数名。extern "C"只能用来导出全局函数,不能导出类和类的成员函数。我想大概是因为C中没有类和类的成员变量吧。

__declspec(dllexport):表示该函数为DLL输出函数,即其他应用程序可以调用该函数


一、静态链接库

静态链接库只生成.lib文件,使用的时候直接用头文件和.lib文件即可。

头文件:


   
   
  1. #pragma once
  2. int Add(int a, int b);
  3. int Sub(int a, int b);
  4. class Student
  5. {
  6. public:
  7. virtual char *GetName() const;
  8. virtual void SetName(char *Name);
  9. virtual int GetAge() const;
  10. virtual void SetAge(int age);
  11. virtual void Output();
  12. private:
  13. char *m_strName;
  14. int m_nAge;
  15. };

源程序:


   
   
  1. #include "stdafx.h"
  2. #include "Sample.h"
  3. #include <iostream>
  4. using namespace std;
  5. int Add(int a, int b)
  6. {
  7. return a + b;
  8. }
  9. int Sub(int a, int b)
  10. {
  11. return a - b;
  12. }
  13. char * Student::GetName() const
  14. {
  15. return m_strName;
  16. }
  17. void Student::SetName(char * Name)
  18. {
  19. m_strName = Name;
  20. }
  21. int Student::GetAge() const
  22. {
  23. return m_nAge;
  24. }
  25. void Student::SetAge(int age)
  26. {
  27. m_nAge = age;
  28. }
  29. void Student::Output()
  30. {
  31. std:: cout << "Name: " << m_strName << std:: endl << "Age: " << m_nAge << std:: endl;
  32. }

测试程序:


   
   
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "Sample.h"
  4. using namespace std;
  5. int main()
  6. {
  7. int sum = 0;
  8. int a = 5;
  9. int b = 3;
  10. sum = Add(a, b);
  11. cout << "sum = a + b = " << sum << endl;
  12. int diff = 0;
  13. diff = Sub(a, b);
  14. cout << "diff = a - b = " << diff << endl;
  15. Student xiaoming;
  16. xiaoming.SetAge( 27);
  17. xiaoming.SetName( "xiaoming");
  18. xiaoming.Output();
  19. int age = xiaoming.GetAge();
  20. char *name = xiaoming.GetName();
  21. cout << "Name:" << name << endl << "Age:" << age << endl;
  22. system( "pause");
  23. return 0;
  24. }



二、动态链接库

生成动态连接库的程序是一致的,但不同是调用方式,动态链接库有动态调用和静态调用两种方式。

在导出函数的时候,两者可以一样,都使用


   
   
  1. #ifdef DLL_IMPLEMENT
  2. #define DLL_API extern "C" __declspec(dllexport)
  3. #else
  4. #define DLL_API extern "C" __declspec(dllimport)
  5. #endif

这个宏。其中个的extern “C”是为了解决C++名字改编的问题,在动态调用时比较有用,因为动态调用需要根据函数名来获取函数地址,如果不加这个,动态调用的函数名将会发生改编,将找不到函数;静态连接就不需要这个。

导出类就不同了,导出类在静态调用动态链接库的情况下,可以直接用__declspec(dllexport)导出类,此时这个类就能正常使用了。在动态调用的情况下,这样直接导出的类是没有地址的,用不了,且它的名字也会发生改变(个人看法)。我一般的用法是使用一个Create函数,Create里面是return new CClass,即这个类的实例化,函数类型是CClass *,整个函数可以理解为CClass class = new CClass。当然动态调用的时候加上了extern “C”,这样可以通过函数名找到函数地址。此时类的成员变量仍然不能用,因为成员函数都没有地址,找不到,因为我不能通过这个类来找到它成员函数的地址,这里把成员函数声明为虚函数,这样就可以了。因为虚函数在类中有一个虚函数表,通过这个虚函数表可以找到类中所有虚函数的地址,这就是我动态调用Dll导出类的方法。

1. 动态链接库的静态调用


头文件文件:需要在预编译器里面定义宏DLL_IMPLEMENT


   
   
  1. #pragma once
  2. #ifdef DLL_IMPLEMENT
  3. #define DLL_API __declspec(dllexport)
  4. #else
  5. #define DLL_API __declspec(dllimport)
  6. #endif // DLL_API
  7. DLL_API int Add(int a, int b);
  8. DLL_API int Sub(int a, int b);
  9. class DLL_API Student
  10. {
  11. public:
  12. char *GetName() const;
  13. void SetName(char *Name);
  14. int GetAge() const;
  15. void SetAge(int age);
  16. void Output();
  17. private:
  18. char *m_strName;
  19. int m_nAge;
  20. };
源文件:


   
   
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "Sample.h"
  4. int Add(int a, int b)
  5. {
  6. return a+b;
  7. }
  8. int Sub(int a, int b)
  9. {
  10. return a - b;
  11. }
  12. char * Student::GetName() const
  13. {
  14. return m_strName;
  15. }
  16. void Student::SetName(char * Name)
  17. {
  18. m_strName = Name;
  19. }
  20. int Student::GetAge() const
  21. {
  22. return m_nAge;
  23. }
  24. void Student::SetAge(int age)
  25. {
  26. m_nAge = age;
  27. }
  28. void Student::Output()
  29. {
  30. std:: cout << "Name: " << m_strName << std:: endl << "Age: " << m_nAge << std:: endl;
  31. }


测试文件:

   
   
  1. // testStaticDll.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include "sample.h"
  6. using namespace std;
  7. int main()
  8. {
  9. int sum = 0;
  10. int diff = 0;
  11. int a = 5;
  12. int b = 3;
  13. sum = Add(a, b);
  14. diff = Sub(a, b);
  15. cout << "sum = a + b = " << sum << endl;
  16. cout << "diff = a - b = " << diff << endl;
  17. 分隔符
  18. Student xiaoming;
  19. xiaoming.SetAge( 27);
  20. xiaoming.SetName( "xiaoming");
  21. xiaoming.Output();
  22. char *sName = xiaoming.GetName();
  23. int nAge = xiaoming.GetAge();
  24. cout << "Name: " << sName << endl << "Age: " << nAge << endl;
  25. system( "pause");
  26. return 0;
  27. }

2. 动态链接库的动态调用

头文件


   
   
  1. #pragma once
  2. #ifdef DLL_IMPLEMENT
  3. #define DLL_API extern "C" __declspec(dllexport)
  4. #else
  5. #define DLL_API extern "C" __declspec(dllimport)
  6. #endif
  7. DLL_API int Add(int a, int b);
  8. DLL_API int Sub(int a, int b);
  9. class Student
  10. {
  11. public:
  12. virtual char *GetName() const;
  13. virtual void SetName(char *Name);
  14. virtual int GetAge() const;
  15. virtual void SetAge(int age);
  16. virtual void Output();
  17. private:
  18. char *m_strName;
  19. int m_nAge;
  20. };
  21. DLL_API Student *Create();

源文件:


   
   
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "Sample.h"
  4. int Add(int a, int b)
  5. {
  6. return a+b;
  7. }
  8. int Sub(int a, int b)
  9. {
  10. return a - b;
  11. }
  12. Student * Create()
  13. {
  14. return new Student;
  15. }
  16. char * Student::GetName() const
  17. {
  18. return m_strName;
  19. }
  20. void Student::SetName(char * Name)
  21. {
  22. m_strName = Name;
  23. }
  24. int Student::GetAge() const
  25. {
  26. return m_nAge;
  27. }
  28. void Student::SetAge(int age)
  29. {
  30. m_nAge = age;
  31. }
  32. void Student::Output()
  33. {
  34. std:: cout << "Name: " << m_strName << std:: endl << "Age: " << m_nAge << std:: endl;
  35. }

测试文件:


   
   
  1. // testDynamicDll.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. #include "sample.h"
  6. #include <iostream>
  7. using namespace std;
  8. typedef int(*Fun)(int , int );
  9. typedef Student *(*CreateFunc)();
  10. int main()
  11. {
  12. HINSTANCE hIns = NULL;
  13. hIns = LoadLibrary(_T( "DllFunc.dll"));
  14. if (hIns == NULL)
  15. {
  16. cout << "Dll加载失败" << endl;
  17. system( "pause");
  18. return 0;
  19. }
  20. Fun Sum = (Fun)GetProcAddress(hIns, "Add");
  21. int sum = 0;
  22. int a = 5;
  23. int b = 3;
  24. sum = Sum(a, b);
  25. cout << "sum = a + b = " << sum << endl;
  26. Fun Diff = (Fun)GetProcAddress(hIns, "Sub");
  27. int diff = 0;
  28. diff = Diff(a, b);
  29. cout << "diff = a - b = " << diff << endl;
  30. CreateFunc CreateStudent = (CreateFunc)GetProcAddress(hIns, "Create");
  31. Student *xiaoming = CreateStudent();
  32. xiaoming->SetAge( 27);
  33. xiaoming->SetName( "xiaoming");
  34. xiaoming->Output();
  35. int age = xiaoming->GetAge();
  36. char *name = xiaoming->GetName();
  37. cout << "Name:" << name << endl << "Age:" << age << endl;
  38. FreeLibrary(hIns);
  39. system( "pause");
  40. return 0;
  41. }





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值