Simulink生成C代码时为何创建MEX文件?能否规避该过程?
Great question—this is a common source of confusion when working with Simulink’s code generation pipeline. Let’s break this down into two parts: why those MEX files are appearing, and how to skip creating them if you don’t need them.
Why MEX Files Are Generated (Even for Normal-Mode References)
Simulink uses MEX files here for pre-code-generation validation and consistency checks, not just for accelerator mode simulations. Here’s the key reasoning:
- Early Error Detection: Before committing to full C code generation, Simulink needs to confirm referenced models behave as expected and their interfaces (inputs/outputs, parameters) play nicely with the main model. A temporary MEX file lets it run a quick lightweight simulation to catch issues early, avoiding wasted time on a full code build for an invalid setup.
- Default Configuration Biases: Many out-of-the-box code generation settings (like the
ModelReferenceBuildConfigurationparameter) prioritize validation. Even with normal-mode references, Simulink defaults to building MEX files to ensure referenced models are code-generable and free of quirks that would break the C code build. - Custom Logic Validation: If your referenced models include MATLAB Function blocks, Stateflow charts, or custom C code, Simulink often compiles MEX files to verify these components can be translated into C correctly. It’s a pre-flight check to avoid unexpected failures during the final code generation step.
How to Skip MEX File Creation During C Code Generation
If you’re confident in your model’s validity and want to skip MEX file generation, adjust your configuration with one of these methods:
1. Using the Simulink GUI
- Open your main model’s Configuration Parameters (press
Ctrl+E). - Navigate to Code Generation > Model Referencing.
- Set Build configuration for referenced models to
NoneorGenerate code only(label may vary slightly by Simulink version). This tells Simulink to skip validation MEX builds and focus solely on C code generation. - Additionally, go to Simulation > Simulation Target and set Accelerator mode to
Offto eliminate any leftover accelerator-related build triggers.
2. Using MATLAB Command Line
For script-based control, use set_param to tweak the relevant parameters:
# Configure main model to skip MEX builds for references set_param('your_main_model_name', 'ModelReferenceBuildConfiguration', 'None'); set_param('your_main_model_name', 'SimulationMode', 'Normal'); # Optional: Configure individual referenced models to avoid MEX builds set_param('your_referenced_model_name', 'BuildConfiguration', 'None');
Edge Cases to Note
If your referenced models include MATLAB Function blocks or Stateflow charts, Simulink may still generate minimal MEX files to validate these components. To reduce this:
- For MATLAB Function blocks: Open the block’s configuration, go to Build Options, and set Build action to
Code Generation Only. This skips simulation-focused MEX builds and only prepares code for C generation.
内容的提问来源于stack exchange,提问作者JohnND

