0

I have 2 sets of code:

  • MATLAB code, and
  • QT C++ code.

I have tried to compile the MATLAB code to a C++ library using the mcc command with the msvc2008 compiler. For my QT C++ code, I'm using mingw to compile. However, when I try to add in the MATLAB converted C++ code there seems to be a lot of problems.

Is it possible to mix these two types of code together? Does anyone have any experiences using a combination of these languages?

! have tried to use Octave but I would rather not recode my MATLAB code. I am trying to look for an alternative to run MATLAB code directly.

NB: I need to use mingw in QT as it is requirement and for matlab mcc command, I only have the choice to use msvc compiler. It would be best if I could make the program as a standalone for portability. The reason why I need to use MATLAB code is because there are some nice matrix math manipulation functions I need and also because it would be easier for me to do research using MATLAB.

Chris
  • 44,602
  • 16
  • 137
  • 156
user1142930
  • 73
  • 1
  • 5

2 Answers2

4

When you compile matlab code using mcc (by default or when using the -m option), you get an executable. So from your C++ file, you can call the matlab executable with the C/C++ command exec.

If you use the -l option (using mcc), you get a shared library, and header. For instance if you type (in matlab):

mcc -l test.m -W cpplib:test.h

This should produce a shared library test.lib or test.so, and a header test.h

In test.h you should have line similar to that:

bool MW_CALL_CONV mlxTest(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

You can call your matlab function using that.

In addition you have to add both shared libraries and headers in you msvc project.

Oli
  • 15,935
  • 7
  • 50
  • 66
  • actually, the problem that i have is that if i were to use mcc command to compile to C, i would need to create a msvc project since it needs some functionality from msvc compiler. i'm actually looking for an answer which could be used with mingw compiler with my QT though. – user1142930 Jan 11 '12 at 11:34
  • It's still possible to do it with mingw and QT, there is no problem. – Oli Jan 11 '12 at 12:10
  • i am meeting with compilation error in QT mingw. [code] example: Program Files\MATLAB\R2010b\extern\include\mclmcr.h:314: error: 'mxInt64' has not been declared [/code] seems like it is some problem that have got to do with mingw or extern by matlab is built for msvc in mind only. – user1142930 Jan 11 '12 at 12:50
  • you have to include mex.h and the corresponding libraries – Oli Jan 11 '12 at 13:05
  • i have tried your "mcc -l test.m -W cpplib:test.h", and added mex.h and still received the mxInt64 error. and anyway, i don't think mex.h is needed as i have compile it to dynamic library file not mex file – user1142930 Jan 11 '12 at 14:31
1

I fixed the mxInt64 and mxUint64 by adding more typedefs to make the code recognize those as signed and unsigned integers 64 bytes long.

madtinker
  • 11
  • 1