2

In its new version 2017b, Matlab has released a new MATLAB Engine API for C++: https://fr.mathworks.com/help/matlab/calling-matlab-engine-from-cpp-programs.html

When I try to use this API, (only to start Matlab at the beginning !!), I have an issue using :

std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();

The compilation is ok, but at the execution :

enter image description here

Here is the call stack:

matlab::engine::initSession --> matlab::engine::startMATLAB --> matlab::engine::startMATLABAsync --> engine_create_session

Now, I use a different way to do my job, but I don't want to let an issue without a solution. Can someone spot the problem with this API?

My configuration :

  • Matlab 2017b
  • Visual Studio 2017
  • Windows 10
FlorianS
  • 106
  • 9
  • Hi, I have the same problem and no idea how to solve, even more, my program was working perfectly and from one day to another this show up. Please let me know if you solve it. Thanks in advance. – JP Cordova May 29 '18 at 02:37
  • Hello @JP Cordova, A magic trick append. Since Matlab 2018a, in Released mode, it's now working. I still face the issue with Debug mode and I have no clue why. – FlorianS May 30 '18 at 07:17
  • Maybe I can help, I unchecked some exceptions from Exceptions Settings, and that did the trick for me, seems like Matlab rise exceptions not only when an error happens and visual studio catch them as errors anyway. To see if this is correct, press continue when the 'exception error' shows up, and if you don't find any problem (memory leaks, etc...) is because of that. To avoid catching those exceptions, press CTR-ALT-E in visual studio (or Debug->Windows->Exception Settings) and test the options. – JP Cordova May 31 '18 at 15:19

3 Answers3

1

Just a few days back, I had the same issue: here

So, I had raised a ticket in MATLAB for technical support. And they suggested to build the project in the following manner which works for me:

  1. Create a project and add the source file.
  2. Go to the project properties, and do the following changes(Make sure to change the architecture to X64):
  3. Under C/C++ General, add the following directory to the field ADDITIONAL INCLUDE DIRECTORIES: C:\Program Files\MATLAB\\extern\include
  4. Under C/C++ Precompiled Headers, select "Not Using Precompiled Headers".
  5. Under Linker General, add the directory to the field ADDITIONAL LIBRARY DIRECTORIES: C:\Program Files\MATLAB\\extern\lib\win64\microsoft
  6. e. Under Configuration Properties ->Debugging Add the following 2 Target paths in the Environment: PATH=C:\Program Files\MATLAB\\bin\win64;
    PATH=C:\Program Files\MATLAB\\extern\bin\win64;
  7. Under Linker Input, add the following names to the field marked ADDITIONAL DEPENDENCIES: libmat.lib
    libMatlabEngine.lib
    libMatlabDataArray.lib

  8. Make sure that the following directory is in the Windows system environment PATH:
    C:\Program Files\MATLAB\\bin\win64;
    C:\Program Files\MATLAB\\extern\bin\win64

  9. Now, build the project and run your application.

1

Like in the comment, I solved the issue by upgrading Matlab from R2017b to R2018b!

WhiteBear
  • 73
  • 2
  • 7
0

I noticed the same issue testing R2020b

while it worked fine with R2020a, a run-time error occurs with R2020b when running

std::unique_ptr matlabPtr = startMATLAB();