MATLAB Answers

1

How can I compile a MATLAB Engine application using Microsoft Visual Studio 9.0 or 10.0 ?

I would like to call MATLAB from my C/C++ application using the Engine interface. However, I would like to compile my project in the Microsoft Visual Studio 9.0 (2008) or Visual Studio 10.0 (2010) environment, instead of compiling it in the MATLAB environment.

4 Answers

Answer by MathWorks Support Team on 14 Jun 2018
Edited by MathWorks Support Team on 14 Jun 2018
 Accepted Answer

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.

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

4. 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:

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

$MATLABROOT\extern\include

b. Under C/C++ Precompiled Headers, select "Not Using Precompiled Headers".

c. Under Linker General, add the 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

d. 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)

7. Under Linker Input, add the following names to the field marked ADDITIONAL DEPENDENCIES:

libmx.lib

libmat.lib

libeng.lib

8. If compiling on 64-bit Windows, change the target to x64 as mentioned on the following link:

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

You should now be able to build the executable.

9. Make sure that the following directory is in the PATH:

(For 32-bit Windows)

$MATLABROOT\bin\win32

(For 64-bit Windows)

$MATLABROOT\bin\win64

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.

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).

10. Build and execute the application.

  4 Comments

Show 1 older comment

I had the same issue when I skipped step 8, so I went back to fix that and it works fine now. Make sure you stick with either a 32-bit setting or a 64-bit setting. This also depends on your MATLAB version.

I used 64-bit MATLAB 2013a and 32-bit VS 2012, the answer is great. The only thing I want to add is in d. 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 we want to use a absolute path, we should type: for example,

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

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

Never forget the semicolon. It took me 3 hours to fix this problem.

Sign in to comment.


Answer by Kristen
on 6 Jul 2014

I have followed the setup and am able to build the program, however when I execute the program I get the following error and am unsure on how to fix it.

    'general.exe': Loaded 'C:\Users\Jakubk2\Documents\Visual Studio 2010\Projects\general\x64\Debug\general.exe', Symbols loaded.
    'general.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libmx.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libmwresource_core.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\msvcp100.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\msvcr100.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libmwi18n.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_date_time-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_filesystem-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_system-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\advapi32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\sechost.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libexpat.dll', Binary was not built with debug information.
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\icuin49.dll', Binary was not built with debug information.
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\icuuc49.dll', Binary was not built with debug information.
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\icudt49.dll', Binary was not built with debug information.
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\icuio49.dll', Binary was not built with debug information.
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libut.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libmwfl.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\imagehlp.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\psapi.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libmwMATLAB_res.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_regex-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\user32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\gdi32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\lpk.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\usp10.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_serialization-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_signals-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_thread-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\boost_log-vc100-mt-1_49.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\ws2_32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\nsi.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\tbb.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\powrprof.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\setupapi.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\cfgmgr32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\ole32.dll', Symbols loaded.
    'general.exe': Loaded 'C:\Windows\System32\devobj.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\tbbmalloc.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\zlib1.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Program Files\MATLAB\R2014a\bin\win64\libeng.dll', Cannot find or open the PDB file
    'general.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\imm32.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\msctf.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\uxtheme.dll', Symbols loaded (source information stripped).
    'general.exe': Loaded 'C:\Windows\System32\clbcatq.dll', Symbols loaded (source information stripped).
    The program '[5224] general.exe: Native' has exited with code 1 (0x1).

any advice on how to resolve this issue?

Thanks Kristen

  1 Comment

for some additional information i am running VS2010 and MATLAB 2014a

Sign in to comment.


Answer by Sergey Antonov on 6 Jul 2014
Edited by Sergey Antonov on 6 Jul 2014

A minute ago I have built my project successfully in Visual Studio 2010 (EXPRESS) x64 config. It was tricky a bit... It doesn't have x64 compiler by default. I had to install MS SDK 7.1. MS SDK 7.1 doesn't install when MSVS redistributables are already installed. Uninstalled them, reinstalled MS SDK... Then I tried to compile project. MSVS wrote error:

The "ConfigurationGeneral" rule is missing from the project.

After that I did some mad things for 2 hours. Finally, I found that it is a bug and all you need is just to install some KB: http://connect.microsoft.com/VisualStudio/feedback/details/519904. It is written, that premium and ultimate versions of MSVS don't have such problem.

  0 Comments

Sign in to comment.


Answer by claudia mendez on 14 Jul 2016

I have done it, and everything its fine, but what do I have to do if I want to change the function?

  2 Comments

The function that is invoked in MATLAB (called using engEvalString) can be changed. If the function is found in the current folder inside MATLAB, that function is invoked. Otherwise, it looks for the function present on the MATLAB path. If you do not make any API changes (i.e. input/output arguments) to your MATLAB function, the C++ engine application does not need to be modified. Otherwise, it had to be modified appropriately.

Sign in to comment.