• 5020阅读
  • 5回复

[讨论]想问下这个动态调用Dll为什么出错.. [复制链接]

上一主题 下一主题
离线ionllovfre
 

只看楼主 倒序阅读 楼主  发表于: 2012-05-01
关键词: DLL
  1. 这个是用vs2010生成的dll
  2. outputDll.h
  3. C/C++ code
  4. // 下列 ifdef 块是创建使从 DLL 导出更简单的// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 OUTPUTDLL_EXPORTS// 符号编译的。在使用此 DLL 的// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将// OUTPUTDLL_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的// 符号视为是被导出的。#ifdef OUTPUTDLL_EXPORTS#define OUTPUTDLL_API __declspec(dllexport)#else#define OUTPUTDLL_API __declspec(dllimport)#endif// 此类是从 outputDll.dll 导出的class OUTPUTDLL_API CoutputDll {public:CoutputDll(void);// TODO: 在此添加您的方法。void writefile();};extern OUTPUTDLL_API int noutputDll;OUTPUTDLL_API int fnoutputDll(void);extern "C"{OUTPUTDLL_API CoutputDll* getClass();}
  5. outputDll.cpp
  6. C/C++ code
  7. // outputDll.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "outputDll.h"#include <fstream>// 这是导出变量的一个示例OUTPUTDLL_API int noutputDll=0;// 这是导出函数的一个示例。OUTPUTDLL_API int fnoutputDll(void){return 42;}// 这是已导出类的构造函数。// 有关类定义的信息,请参阅 outputDll.hCoutputDll::CoutputDll(){return;}void CoutputDll::writefile(){std::ofstream outfile;outfile.open("test.txt");for (int iter = 0; iter != 100; ++iter){outfile<<"This is TEST!"<<std::endl;}outfile.clear();outfile.close();}CoutputDll* getClass(){return new CoutputDll();}
  8. 然后我在Qt上同QLibrary动态调用这个dll:
  9. main.cpp
  10. C/C++ code
  11. #include <QtCore/QCoreApplication>#include <QLibrary>#include <iostream>#include "outputDll.h"using namespace std;typedef CoutputDll* (*fun)();int main(int argc, char *argv[]){QCoreApplication a(argc, argv);QLibrary mylib;char name[10];cout<<"input the dll name"<<endl;while(cin>>name){mylib.setFileName(name);if(mylib.load()){fun getclass = (fun)mylib.resolve("returnClass");if(getclass){CoutputDll *test = getclass();test->writefile();delete test;}elsecout<<"funtion Error"<<endl;}elsecout<<"load Error"<<endl;mylib.unload();}return a.exec();}
  12. 不成功,出现错误提示:
  13. undefined reference to `_imp___ZN10CoutputDll9writefileEv'
  14. collect2: ld returned 1 exit status
  15. 跪求解释!~~

离线ionllovfre

只看该作者 1楼 发表于: 2012-05-01
= =
SB了...我重贴..

这个是用vs2010生成的dll

outputDll.h
  1. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  2. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 OUTPUTDLL_EXPORTS
  3. // 符号编译的。在使用此 DLL 的
  4. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  5. // OUTPUTDLL_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  6. // 符号视为是被导出的。
  7. #ifdef OUTPUTDLL_EXPORTS
  8. #define OUTPUTDLL_API __declspec(dllexport)
  9. #else
  10. #define OUTPUTDLL_API __declspec(dllimport)
  11. #endif
  12. // 此类是从 outputDll.dll 导出的
  13. class OUTPUTDLL_API CoutputDll {
  14. public:
  15. CoutputDll(void);
  16. // TODO: 在此添加您的方法。
  17. void writefile();
  18. };
  19. extern OUTPUTDLL_API int noutputDll;
  20. OUTPUTDLL_API int fnoutputDll(void);
  21. extern "C"
  22. {
  23. OUTPUTDLL_API CoutputDll* getClass();
  24. }

outputDll.cpp
  1. // outputDll.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "outputDll.h"
  5. #include <fstream>
  6. // 这是导出变量的一个示例
  7. OUTPUTDLL_API int noutputDll=0;
  8. // 这是导出函数的一个示例。
  9. OUTPUTDLL_API int fnoutputDll(void)
  10. {
  11. return 42;
  12. }
  13. // 这是已导出类的构造函数。
  14. // 有关类定义的信息,请参阅 outputDll.h
  15. CoutputDll::CoutputDll()
  16. {
  17. return;
  18. }
  19. void CoutputDll::writefile()
  20. {
  21. std::ofstream outfile;
  22. outfile.open("test.txt");
  23. for (int iter = 0; iter != 100; ++iter)
  24. {
  25. outfile<<"This is TEST!"<<std::endl;
  26. }
  27. outfile.clear();
  28. outfile.close();
  29. }
  30. CoutputDll* getClass()
  31. {
  32. return new CoutputDll();
  33. }

然后我在Qt上同QLibrary动态调用这个dll:
main.cpp
  1. #include <QtCore/QCoreApplication>
  2. #include <QLibrary>
  3. #include <iostream>
  4. #include "outputDll.h"
  5. using namespace std;
  6. typedef CoutputDll* (*fun)();
  7. int main(int argc, char *argv[])
  8. {
  9. QCoreApplication a(argc, argv);
  10. QLibrary mylib;
  11. char name[10];
  12. cout<<"input the dll name"<<endl;
  13. while(cin>>name){
  14. mylib.setFileName(name);
  15. if(mylib.load()){
  16. fun getclass = (fun)mylib.resolve("returnClass");
  17. if(getclass){
  18. CoutputDll *test = getclass();
  19. test->writefile();
  20. delete test;
  21. }
  22. else
  23. cout<<"funtion Error"<<endl;
  24. }
  25. else
  26. cout<<"load Error"<<endl;
  27. mylib.unload();
  28. }
  29. return a.exec();
  30. }

不成功,出现错误提示:
undefined reference to `_imp___ZN10CoutputDll9writefileEv'
collect2: ld returned 1 exit status


跪求解释!~~



离线dbzhang800

只看该作者 2楼 发表于: 2012-05-02
因为C++标准没有定义ABI,所以不同编译器(不同厂商、同一厂商的不同版本、甚至同一版本的不同编译选项)编译的C++的单元不能混用。
离线jdwx

只看该作者 3楼 发表于: 2012-05-02
楼上高人啊!
不要混和使用两个编译器。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线benbenmajia

只看该作者 4楼 发表于: 2012-05-02
拿到QT下编译一下
安然.....
离线ionllovfre

只看该作者 5楼 发表于: 2012-05-02
回 2楼(dbzhang800) 的帖子
谢谢!
快速回复
限100 字节
 
上一个 下一个