Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

Product Support

What are my options for interfacing MATLAB code with C/C++ code?


Index

  1. How do I call my existing C code from MATLAB?
  2. How do I call MATLAB from my existing C/C++ code?
  3. How do I convert my M-code into a C/C++ MEX file, stand-alone executable, or shared library?
  4. What products do I need if I want to...?
  5. What products do I need to buy in addition to a compiler?
  6. What flags should I use when compiling my M-code with the MATLAB Compiler?
  7. How can I port the code generated by the MATLAB Compiler from one operating system to another?

Section 1: How do I call my existing C code from MATLAB?

What is a MEX file?
MEX is a built-in utility that will enable you to call your existing C-code in MATLAB by compiling your C code into a dynamically linked MATLAB Executable (“MEX”). MEX-files are dynamically linked subroutines that can be called from within MATLAB as regular MATLAB functions. This requires you to replace your application's main() with a special gateway function -- called “mexFunction” -- to pass inputs and outputs to and from MATLAB. MEX is discussed in the MATLAB External Interfaces Guide. For in-depth examples and other detailed MEX information, see the MEX-files Guide, Technical Note 1605.

To use MEX, you do not need to have any additional MathWorks products; all you need is MATLAB and a supported C compiler. On Windows, a supported compiler (LCC) is distributed with MATLAB. See Technical Note 1601 for a complete list of supported C compilers.

Can I create C++ or Fortran MEX-files?
You can call your Fortran code from MATLAB through MEX. For more information on writing Fortran MEX files, see the External Interfaces Guide. C++ MEX-files, however, are not officially supported, but Tech Note 1605 has some information that will allow you to work with C++ in most cases.

Section 2: How do I call MATLAB from my existing C/C++ code?

If you install the MATLAB C/C++ Math Libraries, you can access a large number of built-in functions in MATLAB from an external C/C++ application. Neither MATLAB nor the MATLAB Compiler is needed to do this. For more information on how to call MATLAB Math Library functions from your C/C++ code, see the MATLAB C/C++ Math Library documentation .

There are no corresponding Math or Graphics Libraries for FORTRAN.

Alternatively, you can call MATLAB from your C/C++ program using the MATLAB Engine. The MATLAB Engine allows C or Fortran programs to communicate with a separate MATLAB process via pipes in UNIX and through ActiveX on Windows. For more information on the MATLAB Engine, see the External Interfaces Guide.

Section 3: How do I convert my M-code into a C/C++ MEX file, stand-alone executable, or shared library?

You can convert your M-code to C/C++ code using the MATLAB Compiler. The translated C/C++ code can then be converted into a MEX file which can be called from MATLAB.

The translated code can also be compiled into a stand-alone application that can run independently of MATLAB, or can be integrated with your existing C/C++ application as a shared library. To run the translated code outside of MATLAB, you need to link it to the MATLAB C/C++ Math Libraries.

Converting M-code to C/C++ code may be advantageous for a number of reasons. See this solution for more information on this topic.

The MATLAB Compiler cannot compile all types of M-code. See this solution for a discussion of the limitations and restrictions of the MATLAB Compiler.

For information on how to get started building MEX files and stand-alone executables, refer to Technical Note 1621. Also, this solution has an example that will lead you through building a shared library.

Section 4: What products do I need if I want to...?

Note: The MATLAB Compiler now includes the MATLAB C/C++ Math and Graphics Libraries. These libraries are no longer supported as separate products. For this reason, when a specific Library is needed or not needed in order to build a specific target, the name of the library will be surrounded by square brackets.

Case 1:

Have: C-code
Want: Create MEX (dynamically linked subroutine)
Why: To run in MATLAB
Need: C-compiler (to compile C code), MATLAB (to run it in)
Don't need: MATLAB Compiler, [C/C++ Math Library]

Case 2:

Have: M-code
Want: Create MEX (dynamically linked subroutine)
Why: Make it run faster, Hide M-code implementation
Need: MATLAB compiler (to compile M code), MATLAB (to run it in), C-compiler
Don't need: [C/C++ Math Library]

Case 3:

Have: M-code
Want: Create stand-alone C/C++
Why: To run outside of MATLAB, To integrate with existing C/C++ application
Need: MATLAB, MATLAB Compiler, [C/C++ Math Library], C/C++ compiler

Case 4:

Have: C/C++ code
Want: Create stand-alone link to MATLAB routines
Why: Don't want to code these routines, since they are already available
Need: [C/C++ Math Library], C/C++ compiler (to compile C code)
Don't need: MATLAB, MATLAB Compiler

Case 5:

Have: C/C++ code
Want: Live link to a MATLAB session and routines
Why: Use MATLAB as a Computational Engine
Need: MATLAB, C/C++ compiler 
Don't need: MATLAB Compiler, [C/C++ Math Library]

Case 6:

Have: C/C++ code; M-code
Want: Create stand-alone C/C++ shared library from M-code
Why: Integrate an existing C/C++ executable with custom MATLAB Code
Need: MATLAB, MATLAB Compiler, C/C++ compiler , [C/C++ Math Library]

Case 7:

Have: C/C++ code; M-code
Want: Create stand-alone COM object from M-code
Why: Integrate an existing C/C++ executable with custom MATLAB Code
Need: MATLAB, MATLAB Compiler, C/C++ compiler, [C/C++ Math Library]
MATLAB COM Builder

Case 8:

Have: C/C++ code; M-code
Want: Create an Excel plug-in from M-code
Why: Integrate an Excel project with custom MATLAB Code
Need: MATLAB, MATLAB Compiler, C/C++ compiler, [C/C++ Math Library]
MATLAB Excel Builder

Section 5: For MATLAB 6.5 (R13), 6.0 (R12), or 5.3 (R11), what products do I need to buy in addition to a compiler?

Minimum Products Needed for an Activity

Activities for All Platforms

You Have: You Want:
MATLAB MATLAB Compiler Simulink Stateflow Stateflow Coder Real-Time Workshop Real-Time Windows Target
C-code CMEX-file x            
C-code C Engine Program x            
C-code C MAT-file x            
Fortran code Fortran MEX-file x            
Fortran code Fortran Engine Program x            
Fortran code Fortran MAT-file x            
M-code CMEX-file x x          
M-code C/C++ stand-alone x x          
C/C++ code C/C++ stand-alone              
C-code CMEX S-function x   x        
M-code M-code S-function x x x        
Fortran code Fortran MEX S-function x   x        
Simulink diagram Real-Time Workshop Target x   x     x  
Simulink diagram Real-Time Windows Target x   x     x x
Simulink diagram Stateflow stand-alone x   x x x    

  • Available products on Release 13 include: MATLAB 6.5, Compiler 3.0 (includes C/C++ Math and Graphics libraries), Simulink 5.0, RTW 5.0, Stateflow 5.0, Stateflow Coder 5.0, RTW Target 2.2, and xPC Target 2.0.
  • Available products on Release 12 include: MATLAB 6.0, Compiler 2.1, C/C++ Math Library 2.1, Simulink 4.0, RTW 4.0, Stateflow 4.0, Stateflow Coder 4.0, RTW Target 2.0, xPC Target 1.1, and the Graphics Library 2.1.
  • Available products on Release 11 include: MATLAB 5.3, Compiler 2.0, C/C++ Math Library 2.0, Simulink 3.0, RTW 3.0, Stateflow 2.0, Stateflow Coder 2.0, RTW Target 1.0, xPC Target 1.0, and the Graphics Library 2.0.

Section 6: What flags should I use when compiling my M-code with the MATLAB Compiler?

During compilation it is often desirable to look at the steps taken by the MATLAB Compiler to generate your code. You can do this by adding a -v flag to any compilation line. To find out more information about the output produced, see this solution.

PROBLEM:
Compile M-code into a C/C++ executable

COMMAND TO USE:
You can compile executable code from your MATLAB code in two different ways.

  1. If you are linking to the C/C++ Math Libraries (and not the Graphics Libraries) the easiest way to compile your code is to use the -m (for a C executable) or -p (for a C++ executable) flag. These flags will compile and link your code in one step.
    For example, if your M-code is a function called foo.m:
    mcc -m foo.m
    or
    mcc -p foo.m 
  2. If you are linking to the C/C++ Math Libraries and the C/C++ Graphics Libraries you can compile your code using the -B sgl (for C code) or -B sglcpp (for C++ code) flag. This will inform the MATLAB Compiler that you would like to link to the Graphics Libraries.
    For example, if your M-code is a function called foo_graphics.m:
     mcc -B sgl foo_graphics.m
    or
    mcc -B sglcpp foo_graphics.m

PROBLEM:
Compile M-code into a MEX function

COMMAND TO USE:
To compile your M-code into a MEX function (C code in a form that can be run from MATLAB like an m-file) you can use the -x flag.

For example, if your function is called foo.m:

mcc -x foo.m 

PROBLEM:
Compile M-code into a C shared library.

COMMAND TO USE:
To compile your M-code into a shared library that can be called by another C/C++ application, you can use the following syntax:

mcc -t -L C -W lib:mylib -T link:lib -h <M-files> libmmfile.mlib
           

For an explanation of what each of these flags do, see the Compiler Flags page in the MATLAB Compiler documentation. For an example on how to create and link against a MATLAB Compiler generated shared library, refer to this solution.

Section 6: How can I port the code generated by the MATLAB Compiler from one operating system to another?

The code generated by the MATLAB Compiler is standard ANSI C, so it can be used on multiple platforms. However, the code produced by the compiler is linked against the MATLAB Math and Graphics Libraries. The binaries for these libraries are different for the PC and UNIX platforms. Therefore, it is necessary to have the MATLAB Math and Graphics Libraries for each operating system where you want to generate your executable.

Porting Code from UNIX to Windows:
If you are generating your C code on a UNIX machine and you have the Math and Graphics Libraries available on Windows, you can use the MBUILD command from the DOS prompt to compile your C into an executable. You will need to copy the C/C++ source files and header files to the PC to do this.

Alternatively, you can follow these instructions to link your libraries into your program using the MSVC IDE:

Note: If you do not have MATLAB installed on the PC (you only have the Math and Graphics Libraries), it may be necessary to change some of the paths to the libraries in the above solution.

Porting Code from Windows to UNIX:
If you are generating your C code on Windows and you have the Math and Graphics Libraries available on a UNIX machine, you should use the MBUILD command from the UNIX prompt to correctly link all of the libraries and source files into an executable program. All you need to copy to the UNIX machine is your C/C++ source files and your header files.

Contact support
E-mail this page
Print this page