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

8 views (last 30 days)
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 Comment
Vincent Huber
Vincent Huber on 6 Oct 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

Sign in to comment.

Answers (2)

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

Abhishek Singh
Abhishek Singh on 6 Mar 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

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!