Generate C/C++ Code
After you prepare your MATLAB® code for code generation, specify input types, and check your code for issues, you can generate standalone C/C++ code or an accelerated MEX function. See the Code Generation Workflow.
Generate Standalone C/C++ Code
To generate standalone C/C++ code:
In the MATLAB Coder™ app, on the Generate Code page, click Generate.
At the command line, pass your entry-point functions to the
codegen
function and use the-config
option to specify a standalone C/C++ output type. See Configure Code Generation and Build Settings.
After you generate standalone C/C++ code, you can:
Create a standalone C/C++ executable and deploy this application on a target platform.
Integrate the generated source code or library into an existing C/C++ project.
Run the generated code on embedded microprocessors by using a toolchain.
Package the generated code for transfer to another development environment.
For details, see Deploy Generated Code and Package Code for Other Development Environments.
Generate an Accelerated MEX Function
To generate an accelerated MEX function:
Review the available optimization strategies. In particular, consider unrolling
for
-loops andparfor
-loops, inlining small functions, and passing variables by reference. See Optimize Generated C/C++ and MEX Code.Disable run-time checks. You should only disable run-time checks if you have checked your MATLAB code for issues by following the Code Generation Workflow. To disable run-time checks, see Control Run-Time Checks.
After you generate an accelerated MEX, you can explore further opportunities for acceleration by using the MATLAB Profiler to identify portions of the generated MEX function that are highly time-intensive. See Profile MEX Functions by Using MATLAB Profiler.
Accelerated MEX functions can be used to speed applications and simulations in a number of different toolboxes. For example:
Real-time audio. See Accelerate Fixed-Point Simulation (Fixed-Point Designer).
Pixel-streaming video processing. See Accelerate Pixel-Streaming Designs Using MATLAB Coder (HDL Coder).
Communications algorithm simulation. See Simulation Acceleration Using MATLAB Coder and Parallel Computing Toolbox (Communications Toolbox).
Standalone application deployment. See Use Generated Code to Accelerate an Application Deployed with MATLAB Compiler.
See Also
Related Examples
- Use an Example C Main in an Application
- Using Dynamic Memory Allocation for an Atoms Simulation
- Generate MEX Code to Accelerate Simulation of Bouncing Balls
- SIL and PIL Verification for Deployment on Raspberry Pi (Embedded Coder)
More About
- Generate C/C++ Code From MATLAB Code Using MATLAB Coder
- C++ Code Generation
- Tracing Generated C/C++ Code to MATLAB Source Code
- Best Practices for Using MEX Functions to Accelerate MATLAB Algorithms