我使用Qt5.14.2创建了一个名为mdll的共享库项目,用MinGW64bit编译,内容如下:
mdll.pro:
QT -= gui
TEMPLATE = lib
DEFINES += MDLL_LIBRARY
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
mdll.cpp
HEADERS += \
mdll_global.h \
mdll.h
# Default rules for deployment.
unix {
target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
mdll.h:
#ifndef MDLL_H
#define MDLL_H
#include "mdll_global.h"
class MDLL_EXPORT Mdll
{
public:
Mdll();
int triple(int _a);
};
#endif // MDLL_H
mdll_global.h:
#ifndef MDLL_GLOBAL_H
#define MDLL_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MDLL_LIBRARY)
# define MDLL_EXPORT Q_DECL_EXPORT
#else
# define MDLL_EXPORT Q_DECL_IMPORT
#endif
#endif // MDLL_GLOBAL_H
mdll.cpp:
#include "mdll.h"
Mdll::Mdll()
{
}
int Mdll::triple(int _a)
{
return _a*3;
}
然后我在我的另一个工程里引用的mdll项目生成的mdll.dll
QLibrary mylib("mdll");
mylib.load();
typedef int (*FunDef)(int); //需要声明函数原型的类型
FunDef myTriple=(FunDef)mylib.resolve("triple");
qDebug()<<myTriple(1);
结果程序异常结束。
我把"qDebug()<<myTriple(1);"这段代码去掉后能正常运行,mylib.isLoad()也输出true,但如果使用myTriple就会异常退出
并且我用dll解析器也不能正常分析这个生成的dll,说是格式未知
请问有人知道怎么解决吗