주요 콘텐츠

Run Polyspace on C/C++ Code Generated from MATLAB Code

R2026b

After generating C/C++ code from MATLAB® code, you can independently check the generated code for:

  • Bugs or defects and coding rule violations by using Polyspace® Bug Finder™.

  • Run-time errors by using Polyspace Code Prover™.

In R2025a: Polyspace does not integrate with the new MATLAB Coder™ app. Use MATLAB scripts to Polyspace analysis as shown in this topic.

Prerequisites

To follow this example:

  • You must have an Embedded Coder® license.

  • You must be familiar with how to use the codegen command. Otherwise, see the MATLAB Coder Getting Started.

  • You must integrate your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink.

Example Files

This tutorial uses the MATLAB Coder example averaging_filter in polyspaceroot\polyspace\examples\doc_cxx\matlab_coder. Here, polyspaceroot is the Polyspace installation folder, such as, C:\Program Files\Polyspace\R2026b. The example shows a Code Prover analysis. You can follow a similar workflow for Bug Finder.

Run Polyspace Analysis Using MATLAB

To run Polyspace analysis, first generate C code from the MATLAB function averaging_filter() as a static library. In the MATLAB Command Window, enter:

% Generate code
matlabFileName = fullfile(polyspaceroot, 'polyspace',...
    'examples','doc_cxx','matlab_coder','averaging_filter.m');
codegenFolder = fullfile(pwd, 'codegenFolder');
codegen(matlabFileName, '-config:lib', '-c', '-args', ...
   {zeros(1,100,'double')}, '-d', codegenFolder);

You can run a Polyspace analysis using one of two ways:

  • Using polyspace.ModelLinkOptions — This class provides access to all Polyspace options and can be used for running a customized analysis.

  • Using pslinkrun — This function allows scripting the analysis quickly without using any classes.

Run a Fully Configured Analysis Using polyspace.Project

Associate a polyspace.ModelLinkOptions object with the generated code. The -codegenfolder option automatically discovers the source files, include paths, and compiler settings from the code generation output.

psprjCfg = polyspace.ModelLinkOptions('-codegenfolder', codegenFolder);

With a polyspace.ModelLinkOptions object, you can configure the Polyspace Code Prover verification in details. For example, tune the verification precision using the property psprjCfg.Precision. A higher precision level takes longer to finish and produces more precise results.

psprjCfg.Precision.OLevel = 2;
psprjCfg.Precision.To = 'Software Safety Analysis level 2';

Configure the analysis to generate a PDF report using the Developer template.

psprjCfg.MergedReporting.EnableReportGeneration = true;
psprjCfg.MergedReporting.CodeProverReportTemplate = 'Developer';
psprjCfg.MergedReporting.ReportOutputFormat = 'PDF';

Specify a folder for the analysis results.

psprjCfg.ResultsDir = fullfile(pwd, 'codeprover_results');

Create a polyspace.Project object. Associate the Configuration property of this object to the options that you previously specified. Run the analysis and open the results.

proj = polyspace.Project;
proj.Configuration = psprjCfg;
cpStatus = proj.run('codeProver');
proj.Results.getResults('readable');

Run a Quick Analysis Using pslinkrun

Create a pslinkoptions object to configure your Polyspace analysis. In the MATLAB Command Window, enter:

opts = pslinkoptions('ec');
Use the properties of the object opts to select a Code Prover verification, store results in a temporary folder, and open the Polyspace user interface when the analysis is complete:
opts.VerificationMode = 'CodeProver';
opts.ResultDir = [tempdir 'results'];
opts.OpenProjectManager = 1;

Run a Polyspace analysis using the preceding configuration:

[polyspaceFolder, resultsFolder] = pslinkrun('-codegenfolder', codegenFolder, opts);
If the analysis is completed without errors, the Polyspace results open automatically.

See Also

|

Topics