MATLAB Answers

1

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

Asked by Matthew Holub on 24 Feb 2019
Latest activity Answered by Abhishek Singh on 6 Mar 2019
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.

  0 Comments

Sign in to comment.

Products


Release

R2018b

2 Answers

Answer by Agah Karakuzu on 28 Feb 2019

I am having the exact same issue!

  0 Comments

Sign in to comment.


Answer by 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

  0 Comments

Sign in to comment.