QT Creator与Matlab混合编程

标签:
qtmatlab |
分类: 编译器或软件使用 |
VS 2008 环境下调用matlab生成的dll
- Win7
- MATLAB 2009b
- VS 2008
在该项目的属性中进行了配置,只对该项目有效。若建新的项目需要重新配置。项目建好后将libmyadd2.lib, libmyadd2.h, libmyadd2.dll拷贝到项目目录下。
首先配置项目属性页/配置属性/C-C++/常规/附加包含目录,请根据自己电脑上软件的安装位置对照设置,如下图所示:
http://s2/bmiddle/4dc31b56n9f9af86f54a1&690<wbr>2008
/链接器/常规/附加库目录,请根据自己电脑上软件的安装位置对照设置,如下图所示:
http://s9/bmiddle/4dc31b56n9f9b03ec5048&690<wbr>2008
// matlabdll2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <mclmcrrt.h>
#include "mclcppclass.h"
#include "libmyadd2.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello world!" << std::endl;
if(
!mclInitializeApplication
{
std::cout << "Could not initialize the application!" << std::endl;
return
-1;
}
// initialize lib
if( !libmyadd2Initialize())
{
std::cout << "Could not initialize libmyadd2!" << std::endl;
return
-1;
}
try
{
// declare and initialize a
mwArray
a(2, 2,
double *aData;
aData = new double[4];
int i;
for( i=0; i<4; ++i)
{
aData[i] = 1.0*i;
}
// print output
std::cout << "a = " << std::endl;
std::cout << aData[0] << ",\t" << aData[1] << std::endl;
std::cout << aData[2] << ",\t" << aData[3] << std::endl;
a.SetData(aData, 4);
std::cout<<a(1,1)<<",\t"<<a(1,2)<<std::endl;
std::cout<<a(2,1)<<",\t"<<a(2,2)<<std::endl;
// declare and initialize b
mwArray
b(2, 2,
b(1,1) = 11.;
b(1,2) = 12.;
b(2,1) = 21.;
b(2,2) = 22.;
std::cout<<b(1,1)<<",\t"<<b(1,2)<<std::endl;
std::cout<<b(2,1)<<",\t"<<b(2,2)<<std::endl;
mwArray
y(2, 2,
mwArray
z(2, 2,
// call the function
myadd2(2, y, z, a, b);
// copy data from mwArray to C++ objects
// allocate outputs
double *yData, *zData;
yData = new double[4];
if( yData == NULL )
{
std::cout << "Failed to allocate memory for yData!" << std::endl;
return -1;
}
zData = new double[4];
if( zData == NULL )
{
std::cout << "Failed to allocate memory for zData!" << std::endl;
return -1;
}
// copy data from mwArray to C++
y.GetData(yData, 4);
z.GetData(zData, 4);
// print output
std::cout << "y = " << std::endl;
std::cout << yData[0] << ",\t" << yData[1] << std::endl;
std::cout << yData[2] << ",\t" << yData[3] << std::endl;
std::cout << "z = " << std::endl;
std::cout << zData[0] << ",\t" << zData[1] << std::endl;
std::cout << zData[2] << ",\t" << zData[3] << std::endl;
// deallocate memory
delete [] aData;
delete [] zData;
delete [] yData;
}
catch( const mwException& e)
{
std::cerr << e.what() << std::endl;
}
// terminate the lib
libmyadd2Terminate();
// terminate MCR
mclTerminateApplication();
return 0;
}
运行结果:
http://s12/middle/4dc31b56n9f9b1a5a54cb&690<wbr>2008
转自:http://blog.sina.com.cn/s/blog_4dc31b560100qp7a.html
- win 7操作系统
- matlab 2009b
- QT Creator 2.0.1
- 首先在matlab环境下编译生成dll
#
# 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上添加一个按钮,取名为显示,如下图所示:
http://s9/middle/4dc31b56n9fb429ddbaf8&690
然后进入显示按钮的clicked()槽函数,编辑完成后的mainwindow.cpp文件为:
#include"mainwindow.h"
#include"ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
#include"myfuncdll.h"
MainWindow::MainWindow(QWidget*parent) :
QMainWindow(parent),
ui(newUi::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
deleteui;
}
typedefbool (MW_CALL_CONV *Fun) (int,mxArray**,mxArray*);//定义函数指针,以备调用
typedefbool ( MW_CALL_CONV*Fun2)(void);
typedefbool ( MW_CALL_CONV*Fun3)(void);
voidMainWindow::on_pushButton_clicked()
{
QLibrarylib("myfuncdll.dll");
QMessageBoxmsg;
if(lib.load())
{
QMessageBox::information(NULL,"OK","DLLload is OK!");
Funopen=(Fun)lib.resolve("_mlfMyFunc"); //援引 _mlfMyFunc() 函数
Fun2init=(Fun2)lib.resolve("_myfuncdllInitialize");//援引 _myfuncdllInitialize() 函数
Fun2termi=(Fun3)lib.resolve("_myfuncdllTerminate");//援引 _myfuncdllTerminate() 函数
if(open) //是否成功连接上 add() 函数
{
QMessageBox::information(NULL,"OK","Linkto 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));//拷贝输出数据
QStringsong=QString::number(_y[1]);
QMessageBox::information(NULL,"OK",song);
termi();//结束DLL库
}
else
QMessageBox::information(NULL,"NO","Linketo Function is not OK!!!!");
}
else
QMessageBox::information(NULL,"NO","DLLis not loaded!");
}
3.运行结果:
http://s3/bmiddle/4dc31b56n9fb43d859142&690
http://s10/middle/4dc31b56n9fb4419bcac9&690
http://s2/middle/4dc31b56n9fb442ac2d51&690
http://www.mathworks.cn/matlabcentral/fileexchange/20927-cc++-and-matlab-types-convertor
l
Include
l
Include
IMPORTANCE: Define
global macro HAS_OPENCV to
enable
l
l
l
However, it is very easy to implement (almost) full support which MIGHT be done in near future.
http://www.mathworks.cn/support/solutions/en/data/1-3VR4WM/?product=CO&solution=1-3VR4WM
This error occurs because the "MATLAB" environment variable
points to a different version of MATLAB than that of the MCR on the
deployment machine. This causes some libraries to be dynamically
loaded from a different version, and leads to this
"nativeThreadRequest" error, among others.
To resolve the issue, unset the "MATLAB" environment variable, or
ensure that it points to the correct version of the MCR on the
deployment machine.
但我在matlab中通过ver与mcrversion查看版本信息,并且查看生成myfuncdll.dll文件中的readme.txt文件,
发现我的matlab compiler runtime
版本与matlab版本是一致的,后来逐步调试,发现当将
http://www.mathworks.cn/matlabcentral/fileexchange/20927-cc++-and-matlab-types-convertor
的源码,此代码中已经开启了matlab引擎,而myfuncdllInitialize()函数则又再一次初始化引擎(此两引擎相同),引擎复用,所以报错,并且经测试发现当系统中同时打开多个matlab进程时,即使它们是同一个版本或者说同一个父亲,各matlab进程独立,包括workspace中的变量、当前目录路径等,之间没有关系影响。