Using a shared library in Qt c++

We are trying to integrate a shared C++ library created by matlab compiler in our project. The library was created from one file containing one (pretty simple) function, and is called downsampleData.
Our C++ project is based on QT 5.7 environment, and links both matlab runtime libraries and the mentioned library using gcc compiler using this command -
g++ -Wl,-rpath,.../Qt5.7.0/5.7/gcc_64/lib -o untitled (many .o files...) L../matlab/runtime/v85/runtime/glnxa64/ -lmwmclmcrrt -L../matlablibrary/downsampleData/for_testing -ldownsampleData -L../Qt5.7.0/5.7/gcc_64/lib -lQt5Widgets -L/usr/lib64 -lQt5Gui -lQt5Network -lQt5Core -lGL -lpthread
The application compiles with no problems (some warning for unused num_indices var, but no errors), and can start running in debug mode. Yet, when we try to initialize the shared library, a segmentation fault occurs, with no more details. Because the library is already compiled, we cannot debug where exactly the segmentation fault occurs. The following lines are used to initialize:
bool t = mclInitializeApplication(NULL,0); bool tt = downsampleDataInitialize();
when debbuging, mclInitializeApplication() succeeds, and downsampleDataInitialize() causes the segmentation fault. When running it with no debugger and no breakpoints, the application runs OK (even if compiled for debugging) - but it seems like the function run by this library doesn't output the wanted values.
mwArray mat(curData->getnpoints(), curData->getnchannels(), mxDOUBLE_CLASS);
QVector<double> markersVec = curData->getMarkers();
mwArray markers(markersVec.size(),1,mxDOUBLE_CLASS);
mwArray datamat(curData->getnpoints(), curData->getnchannels(), mxDOUBLE_CLASS);
mwArray marks;
mxDouble* buffer;
double arr[curData->getnpoints()*curData->getnchannels()];
IEMatrix matt = curData->getData();
for (int i = 0; i < curData->getnpoints();i++){
for (int j = 0; j < curData->getnchannels();j++){
arr[i*curData->getnchannels() + j] = matt(i,j);
}
}
mat.SetData(arr,curData->getnpoints()*curData->getnchannels());
std::cout << "datamat =" << mat << std::endl;
*downsampleData*(2,datamat,marks, mat ,newSampRate, curData->getRecordingSampRate() , curData->getnpoints(), curData->getnchannels(), markers);
std::cout << "datamat =" << datamat << std::endl;
std::cout << "marks =" << marks << std::endl;
in bold you can see the call to the function, and when printing the output 'datamat' is all zeros (not as it should be) and 'marks' is an empty array. The library was compiled on a linux-based OS (ubuntu) with matlab 2015a, and the project is being tested on another ubuntu computer (With only matlab runtime engine on it). The debugger being used is GDB, version 7.11.1
We would appreciate any suggestions about this problem – Is linking with the library done right? Is using the runtime engine and linking to mclmcrrt done right? Should something special be done when compiling the library using the matlab compiler? Any other directions you can offer to check the usage of the library? Is the function call in the right format?
Matlab version being used (Ver output): MATLAB Version: 8.5.0.197613 (R2015a) MATLAB License Number: •••••• Operating System: Linux 3.16.0-57-generic #77~14.04.1-Ubuntu SMP Thu Dec 17 23:20:00 UTC 2015 x86_64 Java Version: Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot™ 64-Bit Server VM mixed mode

1 Comment

This is well past the time when you answered, but when MATLAB compiled libraries start the JVM there will be an access violation that is normally handled by the JVM. So, although it will make you feel squeamish, configure your debugger to ignore access violations and you should be able to run.

Sign in to comment.

Answers (0)

Asked:

on 14 Dec 2016

Commented:

on 29 May 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!