I want to call MATLAB function in my C++ project.
I'm using Matlab R2010a and Visual Studio 2010
First I created a simple matlab function:
function y = foo(x)
y = x+1;
and then I used matlab compiler to compile this function using matlab GUI compiler (File-> new -> Deployment Project and then choose C++ shared Library). It produces this files 2 folders: distrib and src.
distrib contains:
- foo.dll
- foo.h
- foo.lib
src contains :
- foo.cpp
- foo.dll
- foo.exp
- foo.exports
- foo.h
- foo.lib
- foo_mcc_component_data.c
I want to use this file in a C++ application. I tried many times and I didn't find a way. All the ways I found over the internet are using old matlab compiler which produces different files or works on an old version of visual studio.
So please could anyone help me?
What must I do? What files/references must I add and to where? What paths must I define?
maybe it is too late but for the future.
Include
foo.h
.Add C/C++-General-Additional Include Directories the way to the matlab headers (
C:\Program Files (x86)\MATLAB\R2009b\extern\include
).Add
foo.lib
,mclmcrrt.lib
andmclcommain.lib
for Linker in Additional Dependencies.For linker in Additional Library Directories show the way to your matlab libs(
C:\Program Files (x86)\MATLAB\R2009b\extern\lib\win32\microsoft
for 32bit ver (matlab and VS versions should be the same. I had to install the second Matlab 32bit version.)).I added the way to the
foo.lib
in my system path.Before using your library
foo.dll
, you should initialize MCR and library function.After using don't forget:
And some demonstration code, looks like:
The files
foo.h
andfoo.lib
will be required to compile your application. Thefoo.dll
file will need to be shipped with your resulting application, usually in the same directory.If you put the
foo.h
file in the same directory as your source files, you won't need to do anything special to#include "foo.h"
. You can also add the direct path tofoo.lib
in the external linker dependencies.If you want to store these files outside of your project folder and/or re-use these files in other applications, you can read up on VC++ Directories, Projects and Solutions.
Edit: You probably also need to add the MATLAB libraries to your include and library paths. Check out the MathWorks support solution Why do I receive the error 'Could not find include file "mclmcrrt.h"' when trying to compile a stand-alone application?