Segmentation fault when calling function startMATLAB in C(++) code.

2 Ansichten (letzte 30 Tage)
Matthew Holub
Matthew Holub am 24 Feb. 2019
Kommentiert: Vincent Huber am 6 Okt. 2020
I am using Bash on Ubuntu on Windows to compile my code:
g++ -std=c++11 -g -I ../../../../../../Program\ Files/MATLAB/R2018b/extern/include
-L ../../../../../../Program\ Files/MATLAB/R2018b/extern/bin/win64 -pthread testMatlab.cpp
-l:../../../../../../Program\ Files/MATLAB/R2018b/extern/lib/win64/mingw64/libMatlabDataArray.lib
-l:../../../../../../Program\ Files/MATLAB/R2018b/extern/lib/win64/mingw64/libMatlabEngine.lib -o testMatlab.o
I'm using the test code from MATLAB:
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void callSQRT() {
using namespace matlab::engine;
std::cout << "1" << std::endl;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
std::cout << "2" << std::endl;
//Create MATLAB data array factory
matlab::data::ArrayFactory factory;
std::cout << "3" << std::endl;
// Define a four-element typed array
matlab::data::TypedArray<double> const argArray =
factory.createArray({ 1,4 }, { -2.0, 2.0, 6.0, 8.0 });
std::cout << "4" << std::endl;
// Call MATLAB sqrt function on the data array
matlab::data::Array const results = matlabPtr->feval(u"sqrt", argArray);
// Display results
for (int i = 0; i < results.getNumberOfElements(); i++) {
double a = argArray[i];
std::complex<double> v = results[i];
double realPart = v.real();
double imgPart = v.imag();
std::cout << "Square root of " << a << " is " <<
realPart << " + " << imgPart << "i" << std::endl;
}
}
int main() {
callSQRT();
return 0;
}
But I get a segmentation fault at the line "std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();"
Any ideas as to what my issue is?
Thanks.
  1 Kommentar
Vincent Huber
Vincent Huber am 6 Okt. 2020
To compilte my mex function I had to link against
  • libMatlabEngine
  • libMatlabDataArray
  • libmx
  • libmat
  • libmex
  • libut
You'll see with ldd that libMatlabEngine requires some symbols in the others libs

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Agah Karakuzu
Agah Karakuzu am 28 Feb. 2019
I am having the exact same issue!

Abhishek Singh
Abhishek Singh am 6 Mär. 2019
Hi Matthew,
There are couple of things you need to ensure; first ensure compiler you’re using is supported for use with MATLAB, use this link for the same:
On Linux you can use the following commands to know about your compiler:
gcc -v
g95 -v
g77 -v
If this is correct, then check if you’ve set up the build and runtime environment correctly. Use this link to ensure the same:
I hope this would solve the issue

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by