You are on page 1of 5

9/22/13

Call Matlab functions and scripts from an existing C++ application | The Black Hat

Call Matlab functions and scripts from an existing C++ application


Posted on April 24, 2012 by Michael Do you ever wanted the rich functionality of Matlab to use in your familiar C++ applications? Maybe you want to visualize your current data, want to use the Matlab Optimization Toolbox or just use your formerly written Matlab scripts again? Then the following guide is what you could need. Today I tested how to set-up a simple application in both, Visual Studio or Qt Creator, which is connects to a Matlab client to call Matlab functions and change data. 1: Prepare you environment variable If you want to use Matlab functions a Matlab instance has to be started. For doing this the application has to find the Matlab binary and the corresponding dlls. Therefore, the Matlab directory and library directory have to be written into the PATH environment variable of Windows. Lets start, make sure that Matlab is defined correctly in the PATH variable of Windows (Computer->Properties->Advanced system settings->Environment Variables->System variables->Path). If you want to use Matlab functions in a 32-Bit application your entries in path should look like this

E : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ r u n t i m e \ w i n 3 2 ; E : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ b i n ; E : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ b i n \ w i n 3 2 ;

In contrast, if you want to use Matlab in a 64-Bit application set the PATH entries to

E : \ P r o g r a m s \ M a t l a b 2 0 1 1 \ r u n t i m e \ w i n 6 4 ; E : \ P r o g r a m s \ M a t l a b 2 0 1 1 \ b i n ; E : \ P r o g r a m s \ M a t l a b 2 0 1 1 \ b i n \ w i n 6 4 ;

Of course: E:\Programs\Matlab2011 has to be replaced with your Matlab folder.

michael-stengel.com/blog/?p=89

1/5

9/22/13

Call Matlab functions and scripts from an existing C++ application | The Black Hat

Important: Make sure that ONLY A SINGLE Matlab version is written into the PATH variable, which is of course the one you want to use for your application! 32-Bit OR 64-Bit!! Otherwise Visual Studio will be too stupid to take the right one. If you forget to include the Matlab directories in the PATH variable, you will get a missing dlls error when starting the compiled application, e.g. like this:

T h ep r o g r a mc a n ' ts t a r tb e c a u s el i b e n g . d l li sm i s s i n gf r o my o u rc o m p u t e r . T r yr e i n s t a l l i n gt h ep r o g r a mt of i xt h i sp r o b l e m .

2: Set-up your C++ project for using Matlab What comes is important: Regardless if you are using Microsoft Visual Studio or the Qt Creator for compiling your application, you have to start it with Administration rights ! Without administration rights I always got an (non-informative) error.

" P r o g r a mc r a s h e d . "< -Q tC r e a t o r " U n a b l et os t a r ta p p l i c a t i o n . . . "< -V i s u a lS t u d i o

If you are using Microsoft Visual Studio Set up you project as you like. For the first test I habe created an empty project. Then you have to add the include directory and library directory of Matlab to your project settings. For an 32-Bit application add: Project Properties->Configuration Properties->VC++ Directories->Include Directories

E : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ e x t e r n \ i n c l u d e

Project Properties->Configuration Properties->VC++ Directories->Library Directories

E : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ e x t e r n \ l i b \ w i n 3 2 \ m i c r o s o f t

For an 64-Bit application add: Project Properties->Configuration Properties->VC++ Directories->Include Directories

E : \ P r o g r a m s \ M a t l a b 2 0 1 1 \ e x t e r n \ i n c l u d e

michael-stengel.com/blog/?p=89

2/5

9/22/13

Call Matlab functions and scripts from an existing C++ application | The Black Hat

Project Properties->Configuration Properties->VC++ Directories->Library Directories

E : \ P r o g r a m s \ M a t l a b 2 0 1 1 \ e x t e r n \ l i b \ w i n 6 4 \ m i c r o s o f t

As you can see I used two different directories for the 32-Bit and 64-Bit versions of Matlab, since I'm unsure if something could be mixed up if it would be installed into the same directory. For both, 32-Bit and 64-Bit, now add the following basic three Matlab libraries to Project Properties->Linker->Input->Additional Dependencies

l i b e n g . l i b ; l i b m a t . l i b ; l i b m x . l i b ;

If you are using the Qt Creator: Only 32-Bit is supported. The project setup is pretty simple. Go into your .pro file and add 1 I N C L U D E P A T H+ =E : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ e x t e r n \ i n c l u d e 2 L I B S+ =LE : \ P r o g r a m s \ M a t l a b 2 0 1 1 x 8 6 \ e x t e r n \ l i b \ w i n 3 2 \ m i c r o s o f tl l i b e n gl l i b m a tl l i b m x Again, replace "E:\Programs\Matlab2011x86" with your 32-Bit Matlab distribution. 3: Create the application Now I create a simple application for testing the connection to Matlab. I create a new file called main.cpp with the following content: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 # i n c l u d e< s t d i o . h > # i n c l u d e< i o s t r e a m > # i n c l u d e< c m a t h > # i n c l u d e< e n g i n e . h > # i f d e f_ C H A R 1 6 T # d e f i n eC H A R 1 6 _ T # e n d i f u s i n gn a m e s p a c es t d ; i n tm a i n (i n ta r g c ,c o n s tc h a r *a r g v [ ]) { p r i n t f (" \ n H e l l oW o r l d \ n \ n ") ; # i n c l u d e< s t d i o . h > # i n c l u d e< i o s t r e a m > # i n c l u d e< c m a t h > # i n c l u d e< e n g i n e . h > # i f d e f_ C H A R 1 6 T # d e f i n eC H A R 1 6 _ T # e n d i f / / r e d i f i n e dd e f i n i t i o np r o b l e m / / r e d i f i n e dd e f i n i t i o np r o b l e m

michael-stengel.com/blog/?p=89

3/5

9/22/13

Call Matlab functions and scripts from an existing C++ application | The Black Hat

2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4

u s i n gn a m e s p a c es t d ; i n tm a i n (i n ta r g c ,c o n s tc h a r *a r g v [ ]) { E n g i n e* m _ p E n g i n e=e n g O p e n ( N U L L ) ; i f( m _ p E n g i n e= =N U L L ) { p r i n t f ( " E r r o r :M a t l a bn o tf o u n d . " ) ; } e l s e{ p r i n t f ( " S u c c e s s .M a t l a bi st h e r e . " ) ; } } E n g i n e* m _ p E n g i n e=e n g O p e n ( N U L L ) ; i f( m _ p E n g i n e= =N U L L ) { p r i n t f ( " E r r o r :M a t l a bn o tf o u n d . " ) ; } e l s e{ p r i n t f ( " S u c c e s s .M a t l a bi st h e r e . " ) ; } }

Now you can compile the application and run it either directly from Visual Studio or from the command line. There should open a Matlab client in the background and the output in the console should be

S u c c e s s .M a t l a bi st h e r e .

I hope this guide will work out for you. I tested it on my machine with Windows 7 X64 SP1 Qt Creator 2.3.0 Microsoft Visual Studio 2010 Ultimate 10.0.40219.1 SP1 Troubleshooting: If you get an error when compiling the application : - Re-check that you don't mix up 32-Bit with x64 libraries. - Make sure that your project settings are as described above, appropriate to your Matlab installation path. If you get an error when executing the application AFTER having compiled it successfully: - Make sure that you have started your Programming IDE (Visual Studio, Qt Creator, whatever) with Administration grants. - Check that only the one Matlab version (32-Bit OR 64-Bit) you want currently use is written into the system PATH variable as described above ! - Re-check that the PATH entry is correctly as the example above. - Try the Dependency Walker if you still get errors.

michael-stengel.com/blog/?p=89

4/5

9/22/13

Call Matlab functions and scripts from an existing C++ application | The Black Hat

You can find more information on: http://www.mathworks.de/help/techdoc/matlab_external/bp_kqh7.html That's it for now. Greetings! This entry was posted in C++ , Matlab, Qt, Uncategorized, Visual Studio and tagged C++ , Matlab, Qt Creator, Visual Studio by Michael. Bookmark the permalink [http://michael-stengel.com/blog/?p=89] .
2 THOUGHTS ON CALL MATLAB FUNCTIONS AND SCRIPTS FROM AN EXISTING C++ APPLICATION

P on August 9, 2012 at 10:57 pm said:

Hello, I would like to call functions in OpenCV 242 (VS 2008) from within matlab 2010a 32 bit. My program runs without #include mex.h !!! When I add it I get the error which says libmx.lib is missing! I double checked the system path, I have ..\bin and ..\runtime\win32;and I added this path too: ..\bin\win32 I checked all the paths for thousands times!!1 and all of them are correct. I do not what to do?! It is too weird. I am looking forward to hearing from you

Michael on November 23, 2012 at 4:48 pm said:

Did you include ..\Matlab2011x86\extern\lib\win32\microsoft\ ? There are the libraries you need (*.lib). Did you setup you VS project for 32-Bit (x86) ?

michael-stengel.com/blog/?p=89

5/5

You might also like