想问下这个动态调用Dll为何出错.
想问下这个动态调用Dll为什么出错..
这个是用vs2010生成的dll
outputDll.h
outputDll.cpp
然后我在Qt上同QLibrary动态调用这个dll:
main.cpp
不成功,出现错误提示:
undefined reference to `_imp___ZN10CoutputDll9writefileEv'
collect2: ld returned 1 exit status
跪求解释!~~
------解决方案--------------------
fun getclass = (fun)mylib.resolve("returnClass");
------解决方案--------------------
错误提示调用了未定义的类成员函数。说明愿望中的通过调用函数返回一个类的实例未成功。
mylib.resolve("returnClass")改为mylib.resolve("getClass");
这个是用vs2010生成的dll
outputDll.h
- C/C++ code
// 下列 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(); }
outputDll.cpp
- C/C++ code
// outputDll.cpp : 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include "outputDll.h" #include <fstream> // 这是导出变量的一个示例 OUTPUTDLL_API int noutputDll=0; // 这是导出函数的一个示例。 OUTPUTDLL_API int fnoutputDll(void) { return 42; } // 这是已导出类的构造函数。 // 有关类定义的信息,请参阅 outputDll.h CoutputDll::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(); }
然后我在Qt上同QLibrary动态调用这个dll:
main.cpp
- C/C++ code
#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; } else cout<<"funtion Error"<<endl; } else cout<<"load Error"<<endl; mylib.unload(); } return a.exec(); }
不成功,出现错误提示:
undefined reference to `_imp___ZN10CoutputDll9writefileEv'
collect2: ld returned 1 exit status
跪求解释!~~
------解决方案--------------------
fun getclass = (fun)mylib.resolve("returnClass");
------解决方案--------------------
错误提示调用了未定义的类成员函数。说明愿望中的通过调用函数返回一个类的实例未成功。
mylib.resolve("returnClass")改为mylib.resolve("getClass");