Qt C++ 调用matlab生成的 dll

首先根据matlab的版本下载对应的matlab运行库

matlab 各版本运行库下载地址:

https://ww2.mathworks.cn/products/compiler/matlab-runtime.html

-------------------------------------------------------------------------------------

如我下载安装在D:\v90

配置环境变量为:

#-------------------------------------------------
#
# Project created by QtCreator 2018-04-13T17:03:39
#
#-------------------------------------------------

QT       += core gui
QT       += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MatlabAxis
TEMPLATE = app


SOURCES += main.cpp\
        matlabaxis.cpp \
    gameserv.cpp

HEADERS  += matlabaxis.h \
    gameserv.h

FORMS    += matlabaxis.ui

win32: LIBS += -L$$PWD/lib/ -lSix_axis

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

INCLUDEPATH += D:\v90\extern\include
DEPENDPATH += D:\v90\bin\win64


win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -lmclmcrrt
win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -lmclmcr
win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -llibmx
win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -llibmat

win32: LIBS += -L$$PWD/lib/ -lNdxDyPl

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

-------------------------

这些就是matlab运行库要指定的

INCLUDEPATH += D:\v90\extern\include
DEPENDPATH += D:\v90\bin\win64


win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -lmclmcrrt
win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -lmclmcr
win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -llibmx
win32: LIBS += -LD:\v90\extern\lib\win64\microsoft\ -llibmat

-----------------------------------------------

如下matlab头文件如下

//
// MATLAB Compiler: 6.1 (R2015b)
// Date: Fri Apr 13 15:24:19 2018
// Arguments: "-B" "macro_default" "-W" "cpplib:Six_axis" "-T" "link:lib"
// "Six_axis_2.m" 
//

#ifndef __Six_axis_h
#define __Six_axis_h 1

#if defined(__cplusplus) && !defined(mclmcrrt_h) && defined(__linux__)
#  pragma implementation "mclmcrrt.h"
#endif
#include "mclmcrrt.h"
#include "mclcppclass.h"
#ifdef __cplusplus
extern "C" {
#endif

#if defined(__SUNPRO_CC)
/* Solaris shared libraries use __global, rather than mapfiles
 * to define the API exported from a shared library. __global is
 * only necessary when building the library -- files including
 * this header file to use the library do not need the __global
 * declaration; hence the EXPORTING_ logic.
 */

#ifdef EXPORTING_Six_axis
#define PUBLIC_Six_axis_C_API __global
#else
#define PUBLIC_Six_axis_C_API /* No import statement needed. */
#endif

#define LIB_Six_axis_C_API PUBLIC_Six_axis_C_API

#elif defined(_HPUX_SOURCE)

#ifdef EXPORTING_Six_axis
#define PUBLIC_Six_axis_C_API __declspec(dllexport)
#else
#define PUBLIC_Six_axis_C_API __declspec(dllimport)
#endif

#define LIB_Six_axis_C_API PUBLIC_Six_axis_C_API


#else

#define LIB_Six_axis_C_API

#endif

/* This symbol is defined in shared libraries. Define it here
 * (to nothing) in case this isn't a shared library. 
 */
#ifndef LIB_Six_axis_C_API 
#define LIB_Six_axis_C_API /* No special import/export declaration */
#endif

extern LIB_Six_axis_C_API 
bool MW_CALL_CONV Six_axisInitializeWithHandlers(
       mclOutputHandlerFcn error_handler, 
       mclOutputHandlerFcn print_handler);

extern LIB_Six_axis_C_API 
bool MW_CALL_CONV Six_axisInitialize(void);

extern LIB_Six_axis_C_API 
void MW_CALL_CONV Six_axisTerminate(void);



extern LIB_Six_axis_C_API 
void MW_CALL_CONV Six_axisPrintStackTrace(void);

extern LIB_Six_axis_C_API 
bool MW_CALL_CONV mlxSix_axis_2(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);


#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

/* On Windows, use __declspec to control the exported API */
#if defined(_MSC_VER) || defined(__BORLANDC__)

#ifdef EXPORTING_Six_axis
#define PUBLIC_Six_axis_CPP_API __declspec(dllexport)
#else
#define PUBLIC_Six_axis_CPP_API __declspec(dllimport)
#endif

#define LIB_Six_axis_CPP_API PUBLIC_Six_axis_CPP_API

#else

#if !defined(LIB_Six_axis_CPP_API)
#if defined(LIB_Six_axis_C_API)
#define LIB_Six_axis_CPP_API LIB_Six_axis_C_API
#else
#define LIB_Six_axis_CPP_API /* empty! */ 
#endif
#endif

#endif

extern LIB_Six_axis_CPP_API void MW_CALL_CONV Six_axis_2(int nargout, mwArray& axis1, mwArray& axis2, mwArray& axis3, mwArray& axis4, mwArray& axis5, mwArray& axis6, const mwArray& a, const mwArray& b, const mwArray& g, const mwArray& X, const mwArray& Y, const mwArray& Z, const mwArray& num1, const mwArray& num2, const mwArray& num3, const mwArray& num4);

#endif
#endif

调用方式为:

    if (!Six_axisInitialize()) {//先初始化
        QMessageBox::critical(NULL,"init failed","init matlab failed",QMessageBox::Yes);
    return;
    }

        mwArray axis1(1, 1, mxDOUBLE_CLASS);
        mwArray axis2(1, 1, mxDOUBLE_CLASS);
        mwArray axis3(1, 1, mxDOUBLE_CLASS);
        mwArray axis4(1, 1, mxDOUBLE_CLASS);
        mwArray axis5(1, 1, mxDOUBLE_CLASS);
        mwArray axis6(1, 1, mxDOUBLE_CLASS);

        mwArray a(1, 1, mxDOUBLE_CLASS);
        mwArray b(1, 1, mxDOUBLE_CLASS);
        mwArray c(1, 1, mxDOUBLE_CLASS);

        mwArray X(1, 1, mxDOUBLE_CLASS);
        mwArray Y(1, 1, mxDOUBLE_CLASS);
        mwArray Z(1, 1, mxDOUBLE_CLASS);


        mwArray num1(1, 1, mxDOUBLE_CLASS);
        mwArray num2(1, 1, mxDOUBLE_CLASS);
        mwArray num3(1, 1, mxDOUBLE_CLASS);
        mwArray num4(1, 1, mxDOUBLE_CLASS);

        num1(1, 1) = m_num1;
        num2(1, 1) = m_num2;
        num3(1, 1) = m_num3;
        num4(1, 1) = m_num4;

        a(1, 1) = angleX;
        b(1, 1) = angleY;
        c(1, 1) = angleZ;

        X(1, 1) = offsetX;
        Y(1, 1) = offsetY;
        Z(1, 1) = offsetZ;

        axis1(1, 1) = 0;
        axis2(1, 1) = 0;
        axis3(1, 1) = 0;
        axis4(1, 1) = 0;
        axis5(1, 1) = 0;
        axis6(1, 1) = 0;

        int ax1 = 0, ax2 = 0, ax3 = 0, ax4 = 0, ax5 = 0, ax6 = 0;

        Six_axis_2(6, axis1, axis2,axis3, axis4,axis5,axis6, a, b,c,X,Y, Z, num1, num2, num3, num4);//这个6表示输出的参数个数

        axis1.GetData(&ax1, 1);
        axis2.GetData(&ax2, 1);
        axis3.GetData(&ax3, 1);
        axis4.GetData(&ax4, 1);
        axis5.GetData(&ax5, 1);
        axis6.GetData(&ax6, 1);

        ui->lab_axis_1->setText(QString::number(ax1));
        ui->lab_axis_2->setText(QString::number(ax2));
        ui->lab_axis_3->setText(QString::number(ax3));
        ui->lab_axis_4->setText(QString::number(ax4));
        ui->lab_axis_5->setText(QString::number(ax5));
        ui->lab_axis_6->setText(QString::number(ax6));

你可能感兴趣的:(Qt)