标签: qtcreatormatlabdllit | 分类: VC |
经过好多次试验,终于在qt creator环境下成功调用matlab环境下编译的dll,步骤如下。本人的实验环境为:
- win 7操作系统
- matlab 2009b
- QT Creator 2.0.1
- 首先在matlab环境下编译生成dll
function b = myFunc(a)
%MYFUNC Summary of this function goes here
% Detailed explanation goes here
b=a.*a;
end
首先设置编译器,关于编译器的设置在我的一篇博文中《
VS 2008 环境下调用matlab生成的dll》已做阐述,在这里我们选择matlab自带的lcc编译器。
在命令行窗口输入一下命令:
mcc -t -W libhg:myfuncdll -T link:lib -h libmmfile.mlib myFunc.m
编译完成后生成myfuncdll.dll、myfuncdll.h、myfuncdll.lib等文件
可以用VC++ 6.0的depends查看工具查看生成的dll文件的export函数,如下图所示:
2.在qt creator中显式调用dll文件
首先确定系统的环境变量中有以下几个变量:
D:\Software\Matlab2009\R2009b\runtime\win32
D:\Qt\2010.05\bin;
D:\Qt\2010.05\qt\bin;
D:\Qt\2010.05\mingw\bin;
其中目录根据自己的安装路径所定
建立一个Qt Gui Application应用,本实验取名为qttest,将刚才生成的myfuncdll.h、myfuncdll.lib拷贝至工程的qttest文件夹下,将myfuncdll.dll文件拷至运行后的debug文件夹下。
修改工程的pro文件,修改后内容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2011-03-29T17:08:47
#
#-------------------------------------------------
QT += core gui
TARGET = qttest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
libmyadd2.h \
myfuncdll.h
FORMS += mainwindow.ui
OTHER_FILES += \
libmyadd2.lib \
libmyadd2.dll \
myfuncdll.lib \
myfuncdll.dll
INCLUDEPATH += D:/Software/Matlab2009/R2009b/extern/include
INCLUDEPATH += D:/Software/Matlab2009/R2009b/extern/include/win32
LIBS+=-L D:/Software/Matlab2009/R2009b/extern/lib/win32/microsoft -llibmx
LIBS+=-L D:/Software/Matlab2009/R2009b/extern/lib/win32/microsoft -lmclmcr
LIBS+=-L D:/Software/Matlab2009/R2009b/extern/lib/win32/microsoft -lmclmcrrt
在mainwindowui上添加一个按钮,取名为显示,如下图所示:
然后进入显示按钮的clicked()槽函数,编辑完成后的mainwindow.cpp文件为:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <mclmcrrt.h>
#include <mclcppclass.h>
#include <QMessageBox>
#include <QLibrary>
#include <QMetaType>
#include <QDebug>
#include "myfuncdll.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
typedef bool MW_CALL_CONV(*Fun)(int,mxArray**,mxArray*); //定义函数指针,以备调用
typedef bool MW_CALL_CONV(*Fun2)(void);
typedef bool MW_CALL_CONV(*Fun3)(void);
void MainWindow::on_pushButton_clicked()
{
QLibrary lib("myfuncdll.dll");
QMessageBox msg;
if(lib.load())
{
QMessageBox::information(NULL,"OK","DLL load is OK!");
Fun open=(Fun)lib.resolve("_mlfMyFunc"); //援引 _mlfMyFunc() 函数
Fun2 init=(Fun2)lib.resolve("_myfuncdllInitialize");//援引 _myfuncdllInitialize() 函数
Fun2 termi=(Fun3)lib.resolve("_myfuncdllTerminate");//援引 _myfuncdllTerminate() 函数
if (open) //是否成功连接上 add() 函数
{
QMessageBox::information(NULL,"OK","Link to Function is OK!");
init(); //库初始化
double _x[5] = {1, 2, 3, 4, 5}; //输入数组
double _y[5]; //输出数组
mxArray* x = mxCreateDoubleMatrix(1, 5, mxREAL);//创建1*5矩阵
memcpy(mxGetPr(x), (void*)_x, sizeof(_x)); //拷贝输入数据
mxArray* y = mxCreateDoubleMatrix(1, 5, mxREAL);//创建1*5矩阵
open(1,&y,x);
memcpy(_y, mxGetPr(y), sizeof(_y));//拷贝输出数据
QString song=QString::number(_y[1]);
QMessageBox::information(NULL,"OK",song);
termi();//结束DLL库
}
else
QMessageBox::information(NULL,"NO","Linke to Function is not OK!!!!");
}
else
QMessageBox::information(NULL,"NO","DLL is not loaded!");
}
3.运行结果: