Please nominate your Qt Champions for 2021!

Adding Matlab C/C++ Library to Qt Desktop Application

  • Hi All,

    I have created a C shared library using the Matlab C Compiler. I wanted it to be integrated to my Qt Project.

    Before adding the library, I first created a standalone C Executable, to see if the code runs. It does. I get Matlab Plots as well. Everything all right till here. But that's not what i want. I need to change the parameters on from the Qt. Hence, the need for the library.

    Since I am very new in it, I followed the forums and blogs to get it done. This is essentially what I followed.

    My Project file looks something like this
    QT += core gui

    TARGET = seriously
    TEMPLATE = app

    SOURCES += main.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    win32-msvc2008: LIBS += -L"C:/Program Files/MATLAB/MATLAB Compiler Runtime/v711/extern/lib/win32/microsoft/" -lmclmcrrt
    INCLUDEPATH += "C:/Program Files/MATLAB/MATLAB Compiler Runtime/v711/extern/lib/win32/microsoft"
    DEPENDPATH += "C:/Program Files/MATLAB/MATLAB Compiler Runtime/v711/extern/lib/win32/microsoft"
    win32: PRE_TARGETDEPS += "C:/Program Files/MATLAB/MATLAB Compiler Runtime/v711/extern/lib/win32/microsoft/mclmcrrt.lib"

    INCLUDEPATH += "C:/Program Files/MATLAB/MATLAB Compiler Runtime/v711/extern/include"

    win32: LIBS += -L$$PWD/../../serious/src/ -lserious
    INCLUDEPATH += $$PWD/../../serious/src
    DEPENDPATH += $$PWD/../../serious/src
    win32: PRE_TARGETDEPS += $$PWD/../../serious/src/serious.lib

    In the code, serious.lib/dll is added as an external binary compiled by Matlab with VS2008 compiler. The Qt Windows libraries are used for compiling the project with VS2008 compiler.

    When I try to run the code, the application stops working and it gives me following result : exited with code -1073741819. Segmentation fault seems to be the issue.

    Thinking it might be an access violation, I ran Qt as an administrator but to no avail. I don't understand where I am going wrong.
    Please help me with this problem.

    Thanks & Regards

  • Are you initializing the MCR and your library? FYI you don't need to add thee MCR lib path to your INCLUDEPATH.
    I am currently developing a Qt app that uses the MCR. I had a similar issue when I was not initializing the MCR and my library correctly. I am generating a shared C++ library with MATLAB Compiler. You mention that you are generating a C library (why not C++ when you are using Qt?); I'm not sure if my approach fits but this is what works in my case.

    Here is my .pro file:
    TEMPLATE = app
    TARGET = main

    extern/include is where my matlab library header file is

    INCLUDEPATH += include
    "C:\Program Files (x86)\MATLAB\MATLAB Compiler Runtime\v81\extern\include"

    SDV.lib is my MATLAB Compiler generated .lib file

    LIBS += extern\lib\SDV.lib
    "C:\Program Files (x86)\MATLAB\MATLAB Compiler Runtime\v81\extern\lib\win32\microsoft\mclmcrrt.lib"

    SDV.h is my MATLAB Compiler generated .h file

    HEADERS += include/mainWindow.h

    FORMS += UIs/MainWindow.ui


    QT += widgets


    SOURCES += src/main.cpp
    src/mainWindow.cpp \

    RESOURCES += resources.qrc


    release:DESTDIR = ../build/release
    debug: DESTDIR = ../build/debug

    MOC_DIR = $$DESTDIR/moc
    RCC_DIR = $$DESTDIR/qrc
    UI_DIR = include

    I initialize the MCR and my library in the constructor of MainWindow. I terminate both in the destructor. Here is the code for that:
    #include "SDV.h"

    MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
    if (!mclInitializeApplication(NULL,0) || !SDVInitialize())
    std::cerr << "could not initialize the library properly"<<std::endl;



  • How to debug such an application? I got the same problems. I was using MSVC2008 as an IDE on Window XP platform. I could compile and run the application which called matlab routine through dll. But I got an error of "integer divided by zero". I didn't know where the error come from even whether the application entered the matlab subroutine. Could anyone tell me how to debug such an application?

  • I imagine that the divide by zero is happening in your c++ code and not the matlab code. I haven't tried this in a Compiler generated dll from C++ code but here is what happens in MATLAB:

    n = int16(5)

    n =


    d = int16(0)

    d =


    q = n/d

    q =



    ans =


    Check anywhere in your C++ code that you are doing division. Maybe posting your code would help...

Log in to reply