linux--QT create(C++ or C)调用matl

2019-01-22  本文已影响0人  tsslisimin

下面我们开始

Err http://archive.ubuntu.com yakkety/main amd64 Packages 404 Not Found

function [hello] = aaa(dir_name)
hello = dir_name;

end

我们只要for_redistribution_files_only目录下面的.h .so文件即可。记得把aaa.so重命名为libaaa.so

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QLabel>
#include"aaa.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    char temp_dirpath[150] = "hello world";
    if(!aaaInitialize())
    {
       std::cout<<" init error"<<std::endl;
    }
     mwArray mydirpath(temp_dirpath),hello;
     aaa(1,hello,mydirpath);
     char str[50];
     strcat(str, hello.ToString());
     QLabel *test = new QLabel(this);

    test->setText(QString(str));

}

MainWindow::~MainWindow()
{
    delete ui;
}

.pro文件如下:

#-------------------------------------------------
#
# Project created by QtCreator 2019-01-21T10:34:33
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = matlabtest
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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 += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

INCLUDEPATH +=  /home/simin/matlabPro/aaa/for_redistribution_files_only \
                /usr/local/MATLAB/MATLAB_Runtime/v93/extern/include/


LIBS += \
        -L/home/simin/matlabPro/function_test/for_redistribution_files_only \
        -L/home/simin/matlabPro/aaa/for_redistribution_files_only \
        -L/usr/local/MATLAB/MATLAB_Runtime/v93/runtime/glnxa64 \
        -lmwmclmcrrt -laaa

编译运行结果:


image.png
bool MW_CALL_CONV aaaInitialize(void);
extern LIB_aaa_CPP_API void MW_CALL_CONV aaa(int nargout, mwArray& hello, const mwArray& dir_name);

其中int nargout为输出参数的个数。

如果是转C语言,只需在第四步打包的时候选择C share library,然后将MainWindow.cpp对应的部分修改下面所示即可。

    /**C**/
    if(!aaaInitialize())
    {
        std::cout<<"init error"<<std::endl;
    }
     mxArray  *mychar = mxCreateString(temp_dirpath);
     mlfAaa(mychar);
     aaaTerminate();
     aaaPrintStackTrace();
     mclTerminateApplication();
    /**C**/

主要问题汇集:
上一篇 下一篇

猜你喜欢

热点阅读