- libMatlabEngine
- libMatlabDataArray
- libmx
- libmat
- libmex
- libut
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
2020년 10월 6일
To compilte my mex function I had to link against
You'll see with ldd that libMatlabEngine requires some symbols in the others libs
답변 (2개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call MATLAB from C++에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!