Qt

来源:互联网 发布:给自己的淘宝店铺取名 编辑:程序博客网 时间:2024/05/17 02:37
1,生成dll和lib库

在vs2010中新建工程,在向导中选择DLL,如下图所示:

新建两个文件mydll.h和mydll.c

mydll.h代码如下:

 1 #ifndef MYDLL_H 2 #define MYDLL_H 3 #ifdef __cplusplus // 4  extern "C"{ 5  #endif 6  __declspec(dllexport) int myFun(int a,int b); 7  8 #ifdef __cplusplus 9  }10  #endif11 12 #endif

mydll.c代码如下:

1 #include "mydll.h"2 #include <stdio.h>3 4 int myFun(int a,int b)5 {6     printf("myFun is called");7 8     return a+b;9 }

编译运行,在Debug目录下可看到下述文件:

2.在Qt中调用dll和lib库

新建Qt工程LibTest2,将mydll.h文件添加到当前工程中;将mydll.lib和mydll.dll文件复制到工程所在目录;

在LibTest2.pro右键导入外部库,参数选项如下图所示:

在mainwindow.cpp中包含“mydll.h”

在构造函数中添加下述代码:

   int test;      test = myFun(33,33);      qDebug()<<test;

编译运行,结果如下:

可以看到C静态库已被调用。

0 0
原创粉丝点击