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

5 次查看(过去 30 天)
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 个评论
Vincent Huber
Vincent Huber 2020-10-6
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

请先登录,再进行评论。

回答(2 个)

Agah Karakuzu
Agah Karakuzu 2019-2-28
I am having the exact same issue!

Abhishek Singh
Abhishek Singh 2019-3-6
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

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from C++ 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by