QT Creator调用动态链接库实例

#include<iostream>

   #include <QLibrary>
using namespace std;
   int main()
   {
       cout<<"this is a test!"<<endl;
       QLibrary *hello_lib = NULL;
      //写清楚库的路径,如果放在当前工程的目录下,路径为./libhello.so
    hello_lib = new QLibrary("./libsth.so");
    cout<<"this is a test!"<<endl;
      //加载动态库
     hello_lib->load();
     if (!hello_lib->isLoaded())
      {
          cout<<"load libhello.so failed!"<<endl;
          return 0;
     }

      //定义函数指针
      typedef int (*Sub_t)(int,int);

     //resolve得到库中函数地址
     Sub_t Sub = (Sub_t)hello_lib->resolve("sub");
    if (!Sub)
    {
        cout<<"find address error!"<<endl;

    }
    else
        cout<<Sub(1,2)<<endl;
     //卸载库
      hello_lib->unload();
    return 0;
  }

说明,本代码原型来自于网络引用,具体地址稍后补上,感谢分享

posted @ 2016-03-14 09:25  隐念笎  阅读(1155)  评论(0编辑  收藏  举报