Qt - Qt调用VS生成的C静态库

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  #endif
11 
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静态库已被调用。

posted @ 2017-02-16 19:18  暗夜影  阅读(5959)  评论(1编辑  收藏  举报