qt调用vc的dll的问题

snowfield 2011-12-09 11:40:14
要在qt中调用vc生成的动态链接库test.dll。
在pro文件中已经加入了如下语句:

win32: LIBS += -L$$PWD/ -ltest

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/

在编译过程中出现如下错误:
main.cpp:14: 错误:undefined reference to `_imp___Z14Test'
Test中dll中的函数

想问一下,在qt中如果想直接调用vc的dll,应该如何操作?谢谢!
...全文
679 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihang29 2014-05-03
  • 打赏
  • 举报
回复
能不能说得清楚点
lihang29 2014-05-03
  • 打赏
  • 举报
回复
没有搞清楚如何导入动态链接库,不懂
gucunlin 2014-04-22
  • 打赏
  • 举报
回复
静态调用DLL的问题还是没有解决
gucunlin 2014-04-22
  • 打赏
  • 举报
回复
引用 2 楼 xiaoliang_1 的回复:
http://www.qtcn.org/bbs/read.php?tid=21009 楼主你看看这篇文章 你就豁然开朗了
看完了出也没开朗,你自己看完了吗?
snowfield 2011-12-11
  • 打赏
  • 举报
回复
多谢老邓提供详细文档,这下知其所以然了
「已注销」 2011-12-11
  • 打赏
  • 举报
回复
看了下文档,确实需要extern "C"。
void * QLibrary::resolve ( const char * symbol )

Returns the address of the exported symbol symbol. The library is loaded if necessary. The function returns 0 if the symbol could not be resolved or if the library could not be loaded.

Example:

typedef int (*AvgFunction)(int, int);

AvgFunction avg = (AvgFunction) library->resolve("avg");
if (avg)
return avg(5, 8);
else
return -1;


The symbol must be exported as a C function from the library. This means that the function must be wrapped in an extern "C" if the library is compiled with a C++ compiler. On Windows you must also explicitly export the function from the DLL using the __declspec(dllexport) compiler directive, for example:

 extern "C" MY_EXPORT int avg(int a, int b)
{
return (a + b) / 2;
}


with MY_EXPORT defined as

#ifdef Q_WS_WIN
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT
#endif
「已注销」 2011-12-11
  • 打赏
  • 举报
回复
没有必要重新封装。
直接搜索函数符号,返回后调用。
类似:
 QLibrary myLib("mylib");
typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
if (myFunction)
myFunction();
snowfield 2011-12-11
  • 打赏
  • 举报
回复
太感谢各位了!这个问题困扰我很久了!用QLibrary动态加载的方法解决了问题。

我是调用第三方的dll,只有dll和相应的lib文件。设置pro的方法和QLibrary加载的方法都没有成功。
后来,尝试自己在VC下对该dll再次封装,用extern "C" { }把导出的函数括起来,在Qt中用QLibrary加载自己封装后的dll,就可以了。

但通过在pro文件中用win32: LIBS += -L$$PWD/ -ltest的方法还是不行,还没有找到问题所在。但总算可以满足需要了。

有时间再试试念茜推荐的方法。之前也尝试在VS2010上安装qt插件,可能是版本问题,没有成功。改天换vs2008试一下。
念茜 2011-12-11
  • 打赏
  • 举报
回复
最省事得办法:把Qt当做插件安装在VS200X上,用vs200X来编译
xiachm 2011-12-10
  • 打赏
  • 举报
回复
使用QLibrary
刀刀亮 2011-12-10
  • 打赏
  • 举报
回复
http://www.qtcn.org/bbs/read.php?tid=21009
楼主你看看这篇文章 你就豁然开朗了

21,489

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧