Issu using startMATLAB in C++ with “MatlabEngine.h

2019-07-13 18:07发布

问题:

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 :

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

回答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.



回答2:

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