继续 Qt编写模块化插件式应用程序 (上篇) 的内容继续介绍,本节介绍的是Qt编写模块化插件式应用程序 (下篇),我们先来看内容。
2. 编写Animal插件——BilDog和BilPanda项目的实现
现在,让我们来实现两个小插件。BilDog插件很简单,只是汇报下“我是Dog,我正在啃骨头”;BilPanda也是如此——这里仅仅是测试而已,实现的项目中,你可以尽情的发挥——没错,是在遵循IAnimal接口的前提下。
创建BilDog项目,把Bil项目输出的Bil.h、IAnimal.h和Bil.lib加入到工程。
创建Dog类的头文件Dog.h:
-  #ifndef CLASS_DOG_H     
- #define CLASS_DOG_H     
- #include "IAnimal.h"     
- class Dog : public IAnimal     
- {     
- public:     
-     Dog(void);     
-     virtual ~Dog(void);     
- public:     
-     virtual void Eat();     
-     virtual void Run();     
-     virtual void Sleep();     
- };     
- #endif // CLASS_DOG_H    
- #ifndef CLASS_DOG_H  
- #define CLASS_DOG_H  
- #include "IAnimal.h"  
- class Dog : public IAnimal  
- {  
- public:  
-  Dog(void);  
-  virtual ~Dog(void);  
- public:  
-  virtual void Eat();  
-  virtual void Run();  
-  virtual void Sleep();  
- };  
- #endif // CLASS_DOG_H 
创建Dog类的实现文件Dog.cpp:
- #include <QtGui/QMessageBox>     
- #include "Dog.h"     
- Dog::Dog(void)     
- {     
- }     
- Dog::~Dog(void)     
- {     
- }     
- void Dog::Eat()     
- {     
-     QMessageBox::information(NULL, "Hello", "Dog eating ...");     
- }     
- void Dog::Run()     
- {     
-     QMessageBox::information(NULL, "Hello", "Dog running ...");     
- }     
- void Dog::Sleep()     
- {     
-     QMessageBox::information(NULL, "Hello", "Dog sleeping ...");     
- }    
- #include <QtGui/QMessageBox> 
- #include "Dog.h"  
- Dog::Dog(void)  
- {  
- }  
- Dog::~Dog(void)  
- {  
- }  
- void Dog::Eat()  
- {  
-  QMessageBox::information(NULL, "Hello", "Dog eating ...");  
- }  
- void Dog::Run()  
- {  
-  QMessageBox::information(NULL, "Hello", "Dog running ...");  
- }  
- void Dog::Sleep()  
- {  
-  QMessageBox::information(NULL, "Hello", "Dog sleeping ...");  
- } 
调用QT的QMessageBox::information()函数弹出一个信息提示框。
还有一个非常重要的工作,我们得提供一个能够创建(释放)Animal具体对象(这里是Dog)的接口,并且把这些函数导出,让主程序(Test.exe)能够解析这个接口函数,动态创建Animal对象,并访问其功能。
新建BilDog.h文件,输入下面的代码:
- #ifndef BILDOG_H     
- #define BILDOG_H     
- #include "Dog.h"     
-     
- // extern "C" 生成的导出符号没有任何修饰,方便主程序找到它     
- extern "C"    
- {     
-     Q_DECL_EXPORT IAnimal * CreateAnimal();     
-     Q_DECL_EXPORT void ReleaseAnimal(IAnimal * animal);     
- }     
- #endif // BILDOG_H    
- #ifndef BILDOG_H  
- #define BILDOG_H  
- #include "Dog.h"  
-  
- // extern "C" 生成的导出符号没有任何修饰,方便主程序找到它  
- extern "C"  
- {  
-  Q_DECL_EXPORT IAnimal * CreateAnimal();  
-  Q_DECL_EXPORT void ReleaseAnimal(IAnimal * animal);  
- }  
- #endif // BILDOG_H 
这两个函数的工作很简单,直接创建和释放对象即可。 
下面是BilDog.cpp的代码:
- #include "bildog.h"     
- IAnimal * CreateAnimal()     
- {     
-     return new Dog();     
- }     
- void ReleaseAnimal(IAnimal * animal)     
- {     
-     delete animal;     
- }    
- #include "bildog.h"  
-  
- IAnimal * CreateAnimal()  
- {  
-  return new Dog();  
- }  
- void ReleaseAnimal(IAnimal * animal)  
- {  
-  delete animal;  
- } 
至此,一个Animal插件总算完成了。编译,生成BilDog项目,输出BilDog.dll插件文件,以供主程序Test.exe动态调用。
BilPanda项目和BilDog项目类似,在这里就不把代码贴出来了。以后开发Animal插件(即使是第三方)的过程都是如此。
我们不打算输出该项目的.lib文件和那些头文件,因为我们打算让主程序在运行时刻根据需要装载dll插件和调用插件的功能,而不是让主程序项目在编译时就指定具体的插件。
3. 编写客户程序——Test项目的实现
Test项目是一个测试程序项目,但它的角色是主程序,是能使用Animal插件的客户程序。
同样,这个项目用到了Bil共享库,所以得先把Bil项目的几个输出文件导入到Test项目。
我们假设Test主程序是一个对话框,上面有一个编辑框和一个“加载并调用”按钮,终端用户在编辑框中输入Animal插件的文件名(比如BilDog,后缀名可省略,Qt会根据平台判断该查找.dll还是.so),点击“加载并调用”进行共享库的加载,并调用动态创建的IAnimal对象的Eat()函数(当然你可以调用Run()函数或Sleep(),这里仅仅是一个示例)。
下面的函数将被“加载并调用”按钮的触发事件调用:
- // ...     
- #include <QString>     
- #include <QLibrary>     
- #include <IAnimal.h>     
-     
- // ...     
-     
- // strPluginName为插件的名称,可省略后缀     
- void MainDlg::LoadAndAction(QString strPluginName)     
- {     
-     // 加载插件dll     
-     QLibrary lib(strPluginName);     
-     if (lib.load())     
-     {     
-         // 定义插件中的两个导出函数的原型     
-         typedef IAnimal* (*CreateAnimalFunction)();     
-         typedef void (*ReleaseAnimalFunction)(IAnimal* animal);     
-     
-         // 解析导出函数     
-         CreateAnimalFunction createAnimal =      
-                 (CreateAnimalFunction) lib.resolve("CreateAnimal");     
-         ReleaseAnimalFunction releaseAnimal =      
-                 (ReleaseAnimalFunction) lib.resolve("ReleaseAnimal");     
-     
-         if (createAnimal && releaseAnimal)     
-         {     
-             // 创建Animal对象     
-             IAnimal * animal = createAnimal();     
-             if (animal)     
-             {     
-                 // 使用插件功能     
-                 animal->Eat();     
-                 animal->Sleep();     
-                 // 插件使用完毕,删除对象     
-                 releaseAnimal(animal);     
-             }     
-         }     
-         // 卸载插件     
-         lib.unload();     
-     }     
- }    
- // ...  
- #include <QString> 
- #include <QLibrary> 
- #include <IAnimal.h> 
-  
- // ...  
-  
- // strPluginName为插件的名称,可省略后缀  
- void MainDlg::LoadAndAction(QString strPluginName)  
- {  
-  // 加载插件dll  
-  QLibrary lib(strPluginName);  
-  if (lib.load())  
-  {  
-   // 定义插件中的两个导出函数的原型  
-   typedef IAnimal* (*CreateAnimalFunction)();  
-   typedef void (*ReleaseAnimalFunction)(IAnimal* animal);  
-  
-   // 解析导出函数  
-   CreateAnimalFunction createAnimal =   
-     (CreateAnimalFunction) lib.resolve("CreateAnimal");  
-   ReleaseAnimalFunction releaseAnimal =   
-     (ReleaseAnimalFunction) lib.resolve("ReleaseAnimal");  
-  
-   if (createAnimal && releaseAnimal)  
-   {  
-    // 创建Animal对象  
-    IAnimal * animal = createAnimal();  
-    if (animal)  
-    {  
-     // 使用插件功能  
-     animal->Eat();  
-     animal->Sleep();  
-     // 插件使用完毕,删除对象  
-     releaseAnimal(animal);  
-    }  
-   }  
-   // 卸载插件  
-   lib.unload();  
-  }  
- } 
生成Test项目,输出Test.exe。我们把Test.exe、Bil.dll、BilDog.dll、BilPanda.dll放在同一目录,双击运行Test.exe,赶快试下效果吧!注意BilDog.dll或BilPanda.dll依赖于基础接口库Bil.dll,如果系统找不到Bil.dll,将不能加载BilDog.dll或BilPanda.dll,所以请把它们放在同一目录。
四、一些遗憾
DLL的愿望是美好的,只要接口一致,用户可以任意更换模块。但如果不注意细节,很容易陷入它的泥潭中,这就是传说中的DLL Hell(DLL地狱)!
引起DLL地狱问题的主要原因有以下几点:
1. 版本控制不好(主要是接口的版本)
DLL是共享的,如果某程序更新了一个共享的DLL,其它同样依赖于该DLL的程序就可能不能正常工作了!
2. 二制兼容问题(ABI)
即使同一平台,不同编译器(甚至同一编译器的不同版本)编出来的共享库和程序也可能不能协同工作。
二制兼容问题对于C++来说尤其严重。C++的标准是源代码级别的,标准中并没有对如何实现C++作出统一的规定,所以不同的编译器,对标准C++采用不同的实现方式。这些差异主要有:对象在内存中的分配(C++)、构造和析构函数的实现(C++)、重载和模板的实现(C++)、虚函数表结构(C++)、多重继承和虚基类的实现(C++)、函数调用约定(C)、符号修饰(C/C++)等。此外,不同的运行时库(CRT、STL等标准库)也会引起ABI兼容问题。可以说,如果你在编写基于类的共享库,如果接口(指导出类)稍有改变,新的DLL与原程序就可能不协同工作了。
关于二进制兼容问题,大家可以参考KDE官网上的一篇文章《Policies/Binary Compatibility Issues With C++ 》
不过这些都不是大问题,毕竟我们不是编写像Qt一样的通用库。我们引入DLL划分应用程序的模块,目的是减小系统开发和后期升级维护的难度,同时方便项目的管理。如果用户想自己编写插件模块,就得使用我们指定的编译平台和类接口。所以我们仍能从DLL技术中得到很大的实惠。
小结:关于Qt编写模块化插件式应用程序 (下篇)的内容介绍完了,希望本文对你有所帮助!