1

I am struggling to call MATLAB from a simple .cpp program despite the many resources online for this problem.

My Objective:

Use Microsoft Visual Studio 2010 Professional 64-bit to build .cpp program that calls MATLAB Engine for built-in functions and graphs (MATLAB R2013b 64-bit)

What I have done:

In the .cpp file property pages, I have:

  1. Changed the platform to Active(x64)
  2. Added the following paths to the VC++ Directories
    • Executable Directories: C:\Program Files\MATLAB\R2013b\bin
    • Include Directories: C:\Program Files\MATLAB\R2013b\extern\include
    • Reference Directories: C:\Program Files\MATLAB\R2013b\bin\win64
    • Library Directories: C:\Program Files\MATLAB\R2013b\extern\lib\win64\microsoft

Path Edits

  1. Wrote the following program:

    // mEng.cpp : Defines the entry point for the console application.
    // libeng.dll is in C:\Program Files\MATLAB\R2013b\bin\win64
    
    #include "stdafx.h"
    #include <Engine.h>
    #include <matrix.h>
    #include <iostream>
    #include <string>
    #include <cmath>
    
    #pragma comment (lib, "libmat.lib")
    #pragma comment (lib, "libmx.lib")
    #pragma comment (lib, "libmex.lib")
    #pragma comment (lib, "libeng.lib")
    #pragma comment (lib, "libut.lib")
    
    using namespace std;
    
    
    int main()
    {
        Engine *m_pEngine;
        if (!(m_pEngine = engOpen(""))) 
              {
                fprintf(stderr, "\nCan't start MATLAB engine\n");
                return EXIT_FAILURE;
              }
    
        //Now call the MATLAB script through MATLAB Engine
    
              engEvalString(m_pEngine, "script");
              cout << "Hit return to continue\n\n";
              fgetc(stdin);
    
        //Close the MATLAB Engine
    
              engClose(m_pEngine);
              return EXIT_SUCCESS;
    }
    

Errors received:

The program can’t start because libeng.dll is missing from your computer. Try reinstalling the program to fix this problem.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
E. Wash
  • 21
  • 1
  • 6
  • 1
    Have you confirmed that your path includes C:\Program Files\MATLAB\R2013b\bin\win64 ? – user4581301 May 26 '16 at 20:34
  • Thanks for the response. I've edited my post to include a screenshot of my path directories (see the "Path Edits" link). My path does include C:\Program Files\MATLAB\R2013b\bin\win64 in the Reference Directories, is this what you meant? – E. Wash May 28 '16 at 01:30
  • 2
    Meant the system path: where the system is going to look for files at runtime. If the system doesn't know where to look to find libeng.dll, the jig is up. Open a command prompt and type `path` to see if the home of libeng.dll is listed. More info: https://msdn.microsoft.com/en-CA/library/7d83bc18.aspx – user4581301 May 28 '16 at 02:17
  • Ah, that did the trick! I added PATH=C:\Program Files\MATLAB\R2013b\bin\win64 to the Environment field in Project Properties->Debugging – E. Wash May 31 '16 at 18:58
  • That will only hold so long as you are debugging through the IDE. Once you go to run your application on it's own, you'll have a repeat. Recommend adding the dll path to the system PATH. You can find out how to do that for your version of windows with a quick websearch. – user4581301 May 31 '16 at 19:39

2 Answers2

5

I faced the same problem and spent one day to solve it. So here is my answer.

Actually you overdefine different directories (see the "Path Edits" link). It is enough to made just these:

  1. Project Properties -> VC++ Directories -> Library Directories. Add here path C:\Program Files\MATLAB\R2016a\bin\win64 (version and path can differ, but the idea is to est path to the same folder of your MATLAB)
  2. Project Properties -> C/C++ -> General -> Additional Include Directories. Add path C:\Program Files\MATLAB\R2016a\extern\include (your path of this folder)
  3. Project Properties -> Linker -> General -> Additional Library Directories. Add path C:\Program Files\MATLAB\R2016a\extern\lib\win64\microsoft
  4. Project Properties -> Linker -> Input -> Additional Dependencies. Add here: libeng.lib; libmx.lib;
  5. And the last and the most important: add the path of MATLAB Engine dll libraries to your Windows. This step solve your problem. We can do it this way: My Computer -> right click: Settings. Opened window System. Left bottom corner: Advanced System Settings

enter image description here

Advanced Tab

enter image description here

In the Environment variables for your user press Create... button and add this one: Variable name: PATH Variable value: C:\Program Files\MATLAB\R2016a\bin\win64

This path is reference on where is your libeng.dll located. Important moment: it can require to restart VS or even restart computer.

Hope it helps some people, who start to work in MATLAB and C++!

Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102
-4

Follow my notes here:

https://www.mathworks.com/matlabcentral/answers/100603-how-can-i-compile-a-matlab-engine-application-using-microsoft-visual-studio-9-0-or-10-0

I would like to take this opportunity though to re-format the instructions given there:

To compile a MATLAB engine application in the Microsoft Visual Studio 9.0 (2008) or Microsoft Visual Studio 10.0 (2010) environments, perform the steps outlined below:

  1. Open Microsoft Visual Studio and select

    File->New->Project

    and then select:

    Visual C++ -> General -> Empty Project

    Following this, enter the name of the project and its location in the indicated text areas and click OK.

    Note be very careful with this step as it will create the solution folder and put a project folder in there, so you won't want to set this up, get it all working and then change the folder as that will break the solution and you'll have to start all over.

  2. Right-click on the SOURCE FILES folder in the SOLUTION EXPLORER and click

    Add -> "New Item..."

    Choose "C++ file (.cpp)", enter the name of the file (as enginedemo.cpp) as you wish. Click OK.

  3. Copy the code from the source file: engwindemo.c, and paste it into this file enginedemo.cpp. The file engwindemo.c may be obtained from the following location:

    $MATLABROOT/extern/examples/eng_mat
    

    where $MATLABROOT is the MATLAB root directory, and may be determined by entering the command:

    matlabroot
    

    at the MATLAB command prompt.

    I'd suggest that at this point you open it in Notepad and if it has some weird format, fix and save it so it looks like a normal C file when you open it in Notepad (or gedit or whatever).

    This is where things change in Visual Studio and I hope this extends somewhat transparently to VS Code, Eclipse, or whatever IDE you'd like to use. So I will re-number the property change section in roman numerals and fix the order.

    Also be sure to hit ENTER and then APPLY after making every change. Make sure of the change before you go to the next section. Double-check the spelling and MAKE SURE the directories exist on your platform.

  4. Go into the Solution Explorer view, Right click on the project name and select PROPERTIES in the solution explorer to open the project properties. Once this window has opened, make the following changes:

    Go to the Configurations bar at the top of the property page. You most definitely want to start with this setting. Pick a configuration, debug or release, or "all configs" if you want the changes to apply to BOTH debug and release, or you will have to do them all over again for the other configuration. Which may not be such a bad idea, as the individual release options are often more "optimized" versions of the corresponding debug options. Note you can run into serious trouble changing the property values while switching between debug and release modes, don't be surprised if the IDE crashes, the solution database gets corrupt, even the debug won't build and you have to start all over. Get the debug to work first, back-up the entire solution, then "clone" the debug settings to release, and then tweak the release settings one at a time, making sure that the code still builds and runs between each change.

    If compiling to 64-bit Windows, change the target from x86 to x64 & use 64- bit DLLs and library files.

    If this is not in the Configuration options at the very top where it says "Configuration Manager" (as in VS2015), then follow your IDE instructions... as mentioned on the following link:

    https://www.microsoft.com/en-us/download/details.aspx?id=55984

  5. Under

    Configuration Properties ->Debugging

    Add the following Target path in the Environment: (for 32 bit MATLAB)

    PATH=$matlabroot\bin\win32
    

    (for 64-bit MATLAB)

    PATH=$matlabroot\bin\win64 If you want to use an absolute path instead, 
    

    use the following (with a semicolon at the end)

    PATH=C:\Program Files\MATLAB\R2013a\bin\win32; (for 32 bit MATLAB)
    PATH=C:\Program Files\MATLAB\R2013a\bin\win64; (for 64 bit MATLAB)
    

    (probably can skip the "PATH=" here as it doesn't seem to remain there, otherwise this is straight from the MATLAB reference )

  6. Under C/C++ General, add the following directory to the field ADDITIONAL INCLUDE DIRECTORIES:

    $MATLABROOT\extern\include
    

    i.e.

    c:\Program Files\MATLAB\R2019a\extern\include;
    

    Put a semicolon at the end of every line that you enter that is a path. after the .lib files in that section also.

    Under C/C++ Precompiled Headers, select "Not Using Precompiled Headers". (in release mode this was set to "yes")

    Under Linker General, add the library directory to the field ADDITIONAL LIBRARY DIRECTORIES: (For 32-bit Windows)

    $MATLABROOT\extern\lib\win32\microsoft
    

    (For 64-bit Windows)

    $MATLABROOT\extern\lib\win64\microsoft
    

    i.e. c:\Program Files\MATLAB\Rxxxxx\extern\lib\win(32|64)\microsoft;

  7. Under Linker Input, add the following library names to the field marked ADDITIONAL DEPENDENCIES: (use this string in front of whatever else is in there) libeng.lib;libmat.lib;libmx.lib; I don't know if the order really matters, but the original reference listed them in reverse alphabetical order. "I've heard it both ways".

    Ok, now you're done changing the VS project property pages, save the changes (as you've done all along by clicking "Apply" after each change) and click Ok. The property pages window will close. At this point your code will build but still won't call Matlab. Back-up the solution again.

    Now change the DOS/WINDOWS ENVIRONMENT PATH STRING. I would save the solution, close VS, back it up, make the PATH changes and reopen VS and try to build your program and run it and have it work. The PATH string must be changed so that the bin\win(32:64) directory is the first instance related to Matlab on the path.

    Go in the "advanced system settings" \ Environment To do this, check the Windows system path by clicking on Start -> Right click on Computer -> Properties -> Advanced System Settings -> Environment variables -> System Variables -> Open "Path" for editing. and modify the PATH variable as so:

  8. Make sure that the following directory is in the PATH: (For 32-bit Windows)

    $MATLABROOT\bin\win32
    

    (For 64-bit Windows)

    $MATLABROOT\bin\win64
    

    It does not have to be the first directory in the path, but it needs to be the first MATLAB directory in the path

    PATH=c:\;....;c:\Program Files\MATLAB\Rxxxxx\bin\win(32/64);
    

    The matlab.exe is in \bin; you might want that on the path also just not before the \bin\win64 entry...

    Note: If the machine has more than one MATLAB installed, the directory for the target platform must be ahead of any other MATLAB directory (for instance, when compiling a 64-bit application, the directory in the MATLAB 64-bit installation must be the first one on the PATH).

    Also make sure that your target install of MATLAB is registered as a COM server:

    https://www.mathworks.com/help/matlab/ref/regmatlabserver.html For Matlab 2020a & later, try comserver('query') and comserver('register'). For 2019 & earlier use regmatlabserver() (run Matlab as administrator).

  9. Build and execute the application.

    So I include a simple app that should demonstrate the functionality: #include "stdafx.h" #include "engine.h" #include

    using namespace std;
    int main()
    {
    Engine *eng;
    eng=engOpen("");    // put a breakpoint here
    cout << "...now you see it...\n"
    engClose(eng);      // put a breakpoint here
    cout << "...now you don't!\n"
    return 0;
    }
    
tripleee
  • 175,061
  • 34
  • 275
  • 318