Issue using startMATLAB in C++ with "MatlabEngine.hpp" in r2021b
29 views (last 30 days)
Show older comments
Hello,
This is an identical issue to this and this, but essentially i have followed the Cmake instructions to run the Matlab API in c++, ie found the package, linked the engine libraries etc. It seems to build and link just fine but on running it will crash with an access violation on 0x0 address when Matlab calls 'cpp_engine_create_session()'. Again pretty much identical to similarly raised issues but i can't find an answer anywhere as to what causes this and how to fix it?
3 Comments
Joe Elsom
on 27 Jul 2022
Hi,
Not a solution, but some info that may help us all with this. using R2022a.
I've been facing the same issue. I found my code runs fine if the dependency libMatlabEngine.dll is NOT in my build folder (i.e the folder where the exe is run). However, if the exe is run on a different machine, it fails to start since libMatlabEngine.dll is not found. Adding the .dll on the 2nd machine results in the access violation.
So, it appears this issue happens when the exe is using libMatlabEngine.dll directly. I'm not actually sure what the code is using when libMatlabEngine.dll is NOT in the build folder.
Joe Elsom
on 27 Jul 2022
More Info :
After some digging i found this post refering to the matlab C++ engine requiring a valid installation of Matlab on the machine. I experiemented with this and found a few things.
1) To run the C++ build on the build machine, make sure <matlabroot>\<version>\extern\bin\win64 is on the PATH. This can be set via the environment variables. When the above is on the path, do not include libMatlabEngine.dll or libMatlabDataArray.dll in the build folder. The call to matlab::engine::startMatlab() should work, since it will find and use the .dlls inside the local Matlab install via the environment variable. Note the code will not work on a 2nd machine unless it also has a valid Matlab install + the PATH envrionment variable set properly.
2) if libMatlabEngine.dll is included in the build folder (i.e via a post-build event command xcopy) on when the call to matlab::engine::startMatlab() is made, it will try to use the local libMatlabEngine.dll in the build folder. In my case, this causes the access violation exception. I assume, based on the linked post above this is because libMatlabEngine.dll is looking for other Matlab dependencies usually relative to it in the local Matlab install folder (the environment variable set in #1 above).
3) Setting an incorrect environment variable (i.e pointing to the .\extern\bin\win64 folder of a Matlab Runtime install) will raise the exception that libMatlabEngine.dll is not found. This is becasue the Runtime doesn't include it. I copied it to the folder and got a different exception. I assume becasue there are Matlab install dependencies missing when you're only using the Runtime.
So, at the moment at least it looks like it is not possible to run the C++ Engine API on a 2nd machine wihtout a valid install of Matlab (the Runtime doesn't suffice). Additionally, it is possible to run the C++ Engine API locally on the build machine by setting a PATH environement variable that points to the correct folder in a valid Matlab install.
Hopefully this is helpful. I'm still after a proper solution for distributing C++ builds that rely on Matlab though. I guess the Matlab compiler will be required.
Answers (3)
Scott Dattalo
on 12 Aug 2022
Edited: Scott Dattalo
on 12 Aug 2022
Thanks @Joe Elsom. I was have the same issue with using locally referenced libMatlab*.dll's. Moving those out of the way fixed my problem. If those DLL's are locally referenced, then a problem occurs when the C++ app invokes startMATLAB():
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
I'm using MATLAB2019a and VS2019.
1) System environment variables. I had to add this to the beginning of my Windows PATH
C:\Program Files\MATLAB\R2019a\extern\bin\win64
2) The VS2019 Configuration Platform needs to be set to "x64". You can use either the "Release" or "Debug" configurations.
3) In Configuration Properties -> Debugging, I added.
C:\Program Files\MATLAB\R2019a\extern\bin\win64\;%PATH%
*note* - (1) and (2) seem the same. I should go back and see if both are needed.
4) Configuration Properties -> C/C++ -> General
Additional Include Directories set to C:\Program Files\MATLAB\R2019a\extern\include
5) Configuration Properties -> Linker -> General
Additional Library Directories set to C:\Program Files\MATLAB\R2019a\extern\lib\win64\microsoft
6) Configuration Properties -> Linker -> Input
Additional Dependencies prepend with -> libMatlabEngine.lib;libMatlabDataArray.lib;
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void callSQRT() {
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
//Create MATLAB data array factory
matlab::data::ArrayFactory factory;
// Define a four-element typed array
matlab::data::TypedArray<double> const argArray =
factory.createArray({ 1,4 }, { -2.0, 2.0, 6.0, 8.0 });
// 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() {
std::cout << "Starting\n";
callSQRT();
return 0;
}
Scott
3 Comments
Gerrith
on 11 Dec 2024
@Joe Elsom do you know if this is still the case for MATLAB2024b? Does the MCR now include the MATLAB Engine API for C++?
@Scott Dattalo thanks for your solution. Worked for me, after setting the enviroment variables and the Visual Studio project settings, I've just needed to restart Visual Studio for the effects to take place. I'm using MATLAB 2024b and VS2019
Xiaodong Liu
on 27 Sep 2023
The solution is very simple. After setting include and library folders, DONOT copy ANY dll files to your own build folder. Then, it will work.
0 Comments
gen
on 11 Jan 2024
Please "cd" to ${matlabroot}\extern\bin\win64, then invoke your testVal.exe using absolute path. That will do.
0 Comments
See Also
Categories
Find more on Call MATLAB from C++ in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!