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

174 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Jun 2018
Edited: MathWorks Support Team on 14 Jun 2018
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:
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.
  7 Comments
Indhu Priyadharshini Govindasamy
Error :"C2371 'size_t': redefinition; different basic types"
I have followed the above steps and also changed the configuration settings to 64 bit in visual studio and matlab
and also i have added the matlab path in environment variable path but still i am getting this error.
Could anyone please help me to fix this?

Sign in to comment.

More Answers (3)

Kristen
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

Sergey Antonov
Sergey Antonov on 6 Jul 2014
Edited: 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.

claudia mendez
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?
  3 Comments
Samuel Gray
Samuel Gray on 22 Sep 2021
...the string can be changed that is passed to engEvalString.
That string is equivalent to the string parsed by Matlab.
i.e. you want to run foo.m in Matlab by calling it from C, call engEvalString() with "foo" and the engine handle from your C code. If you want to get the function output back into C from Matlab, call the buffered i/o functions as shown in the other examples. They are pretty simple to understand and use at least for basic operations like text and binary i/o, but they're a good start to more complex i/o.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!