Matlab Engine Application with Qt



  • Hello everyone,

    I am trying to create a Qt application that uses the Matlab engine to perform calculations and execute scripts.
    My build system has the following parameters that I think are relevant :

    Matlab 2017a
    Ubuntu 16.04
    gcc 5.4.0
    Qt5.10.0

    The application I am currently trying to compile is very simple

    #include <QcoreApplication>
    #include <QDebug>
    #include "engine.h"
    #include "matrix.h"
    
    int main(int argc, char *argv[])
    {
        Engine* m_engine;
    
        m_engine = engOpen("");
    
        if (!(m_engine = engOpen(""))) {
            qDebug()<<"\nCan't start MATLAB engine\n";
            exit(-1);
        }
    }
    

    The pro file I am using is the following :

    QT -= gui
    
    CONFIG += c++11
    CONFIG -= app_bundle
    
    
    MATLAB_ROOT_PATH = /usr/local/MATLAB/R2017a
    
    MATLAB_INCLUDE_PATH = $${MATLAB_ROOT_PATH}/extern/include
    MATLAB_LIB_PATH     = $${MATLAB_ROOT_PATH}/bin/glnxa64
    MATLAB_SYS_DIR = $${MATLAB_ROOT_PATH}/sys/os/glnxa64
    
    LIBS += -L$${MATLAB_LIB_PATH} -lmx -lmex -lmat -lm -leng
    LIBS += -L$${MATLAB_SYS_DIR} -lstdc++
    
    INCLUDEPATH += $$MATLAB_INCLUDE_PATH
    DEPENDPATH  += $$MATLAB_INCLUDE_PATH
    
    QMAKE_RPATHDIR += $$MATLAB_LIB_PATH
    QMAKE_RPATHDIR += $$MATLAB_SYS_DIR
    
    DEFINES += QT_DEPRECATED_WARNINGS
    SOURCES += main.cpp
    

    The above code compiles, but I get the following error during execution from both the Qt Creator and the system console :

    ./matlab_test: /usr/local/MATLAB/R2017a/bin/glnxa64/libQt5Core.so.5: no version information available (required by ./matlab_test)
    ./matlab_test: /usr/local/MATLAB/R2017a/bin/glnxa64/libQt5Core.so.5: no version information available (required by ./matlab_test)
    ./matlab_test: relocation error: ./matlab_test: symbol qt_version_tag, version Qt_5.10 not defined in file libQt5Core.so.5 with link time reference
    

    I have looked into this problem for a while now and problem was that I received runtime error regarding the stdc++ library which was not the same as the compilers.

    Replacing the linking to the stdc++ library with the following

    LIBS += -L/usr/lib/x86_64-linux-gnu -lstdc++
    

    results in the following compilation error :

    g++ -c -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I../matlab_test -I. -I/usr/local/MATLAB/R2017a/extern/include -I../../../../Qt/5.10.0/gcc_64/include -I../../../../Qt/5.10.0/gcc_64/include/QtCore -I. -I../../../../Qt/5.10.0/gcc_64/mkspecs/linux-g++ -o main.o 
    g++ -Wl,-O1 -Wl,-rpath,/home/stef/Qt/5.10.0/gcc_64/lib -Wl,-rpath,/usr/lib/x86_64-linux-gnu -Wl,-rpath,/usr/local/MATLAB/R2017a/bin/glnxa64 -Wl,-rpath,/usr/local/MATLAB/R2017a/sys/os/glnxa64 -Wl,-rpath,/home/stef/Qt/5.10.0/gcc_64/lib -o matlab_test main.o   -L/usr/local/MATLAB/R2017a/bin/glnxa64 -lmx -lmex -lmat -lm -leng -L/usr/lib/x86_64-linux-gnu -lstdc++ -L/home/stef/Qt/5.10.0/gcc_64/lib -lQt5Core -lpthread 
    /usr/local/MATLAB/R2017a/bin/glnxa64/libmwi18n.so: undefined reference to `icu_56::UnicodeString::UnicodeString(char16_t const*, int)'
    /usr/local/MATLAB/R2017a/bin/glnxa64/libmwi18n.so: undefined reference to `UCNV_FROM_U_CALLBACK_ESCAPE_WITH_INVISIBLE_CHARS_56'
    /usr/local/MATLAB/R2017a/bin/glnxa64/libmwi18n.so: undefined reference to `UCNV_FROM_U_CALLBACK_STOP_WITH_INVISIBLE_CHARS_56'
    /usr/local/MATLAB/R2017a/bin/glnxa64/libmwi18n.so: undefined reference to `icu_56::UnicodeString::UnicodeString(char16_t const*)'
    /usr/local/MATLAB/R2017a/bin/glnxa64/libmwi18n.so: undefined reference to `icu_56::UnicodeString::doCompare(int, int, char16_t const*, int, int) const'
    Makefile:244: recipe for target 'matlab_test' failed
    collect2: error: ld returned 1 exit status
    make: *** [matlab_test] Error 1
    16:18:26: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project matlab_test (kit: Desktop Qt 5.10.0 GCC 64bit)
    When executing step "Make"
    

    Mathworks states that they officially support gcc 4.9, but I think this is true when compiling from MATLAB directly. I have also tried compiling by linking the libraries in the Matlab folder to the system ones instead, but I get similar errors.

    Anyone have any idea? Do you think it is a compiler issue? Do you think it could be solved by compiling Qt with gcc 4.9 and then trying again? Am I not linking the libraries correctly in the qmake .pro file?

    Thank you for your time

    PS : The ldd command on the executable when I can get it to compile gives me the following

    linux-vdso.so.1 =>  (0x00007ffc42980000)
    libeng.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libeng.so (0x00007f992c48b000)
    libstdc++.so.6 => /usr/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6 (0x00007f992c178000)
    libQt5Core.so.5 => /usr/local/MATLAB/R2017a/bin/glnxa64/libQt5Core.so.5 (0x00007f992ba41000)
    libgcc_s.so.1 => /usr/local/MATLAB/R2017a/sys/os/glnxa64/libgcc_s.so.1 (0x00007f992b82b000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f992b449000)
    libut.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libut.so (0x00007f992b165000)
    libmx.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmx.so (0x00007f992add3000)
    libmat.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmat.so (0x00007f992ab78000)
    libboost_system.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_system.so.1.56.0 (0x00007f992a975000)
    libmwcpp11compat.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmwcpp11compat.so (0x00007f992a757000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f992a44d000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f992a230000)
    /lib64/ld-linux-x86-64.so.2 (0x000055e67200a000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f992a016000)
    libicui18n.so.56 => /usr/local/MATLAB/R2017a/bin/glnxa64/libicui18n.so.56 (0x00007f9929b8e000)
    libicuuc.so.56 => /usr/local/MATLAB/R2017a/bin/glnxa64/libicuuc.so.56 (0x00007f99297f1000)
    libicudata.so.56 => /usr/local/MATLAB/R2017a/bin/glnxa64/libicudata.so.56 (0x00007f9927e0b000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9927c06000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f99279fe000)
    libmwi18n.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmwi18n.so (0x00007f99276ac000)
    libmwfl.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmwfl.so (0x00007f99272f9000)
    libmwresource_core.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmwresource_core.so (0x00007f99270f7000)
    libmwfoundation_usm.so => /usr/local/MATLAB/R2017a/bin/glnxa64/libmwfoundation_usm.so (0x00007f9926ed6000)
    libboost_chrono.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_chrono.so.1.56.0 (0x00007f9926ccf000)
    libboost_date_time.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_date_time.so.1.56.0 (0x00007f9926abe000)
    libboost_filesystem.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_filesystem.so.1.56.0 (0x00007f99268a8000)
    libboost_log.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_log.so.1.56.0 (0x00007f99265ca000)
    libboost_regex.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_regex.so.1.56.0 (0x00007f99262ba000)
    libboost_thread.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_thread.so.1.56.0 (0x00007f9926099000)
    libexpat.so.1 => /usr/local/MATLAB/R2017a/bin/glnxa64/libexpat.so.1 (0x00007f9925e70000)
    libicuio.so.56 => /usr/local/MATLAB/R2017a/bin/glnxa64/libicuio.so.56 (0x00007f9925c63000)
    libtbb.so.2 => /usr/local/MATLAB/R2017a/bin/glnxa64/libtbb.so.2 (0x00007f9925a18000)
    libtbbmalloc.so.2 => /usr/local/MATLAB/R2017a/bin/glnxa64/libtbbmalloc.so.2 (0x00007f99257c5000)
    libboost_signals.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_signals.so.1.56.0 (0x00007f99255af000)
    libhdf5_hl.so.8 => /usr/local/MATLAB/R2017a/bin/glnxa64/libhdf5_hl.so.8 (0x00007f992537e000)
    libhdf5.so.8 => /usr/local/MATLAB/R2017a/bin/glnxa64/libhdf5.so.8 (0x00007f9924eed000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f9924cb4000)
    libpam.so.0 => /lib/x86_64-linux-gnu/libpam.so.0 (0x00007f9924aa6000)
    libboost_serialization.so.1.56.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libboost_serialization.so.1.56.0 (0x00007f9924846000)
    libunwind.so.8 => /usr/local/MATLAB/R2017a/bin/glnxa64/libunwind.so.8 (0x00007f9924626000)
    libssl.so.1.0.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libssl.so.1.0.0 (0x00007f99243b5000)
    libcrypto.so.1.0.0 => /usr/local/MATLAB/R2017a/bin/glnxa64/libcrypto.so.1.0.0 (0x00007f9923f6e000)
    libaudit.so.1 => /lib/x86_64-linux-gnu/libaudit.so.1 (0x00007f9923d46000)
    


  • According to Mathworks "Build C++ Engine Programs" the following libraries should be used in Linux:

    Linux Libraries
    Engine library — matlabroot/extern/bin/glnxa64/libMatlabEngine.so
    MATLAB Data Array library — matlabroot/extern/bin/glnxa64/libMatlabDataArray.so
    Additional library — pthread

    Could it be in your pro file you need to add extern to this path?

    MATLAB_LIB_PATH = $${MATLAB_ROOT_PATH}/bin/glnxa64



  • @shwoods said in Matlab Engine Application with Qt:

    libMatlabEngine

    These libraries were included with MATLAB 2017b. The previous MATLAB versions use the engine.h and matrix.h.

    I have been in contact with mathworks and as a first step towards solving the problem was to use Qt 5.5 while linking to the libstdc++.so located in the $$MATLAB_SYS_DIR. The problem then is that matlab engine does not start.

    I will update you if I find more


  • Qt Champions 2017

    @stevosteve
    Hi
    You must likely have checked that but just in case.
    Did you try LDD on libMatlabEngine to see if anything missing ?



  • Hi stevosteve,

    Just wondering if your problem solved? I am quite new to Qt and trying to call some Matlab functions, while I think I have the same issue as yours, do you have any idea where it went wrong?

    Thank you very much,

    Sussi


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.