This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

C++ Object Methods as Legacy Functions

This example shows you how to use the Legacy Code Tool to integrate legacy C++ object methods.

The Legacy Code Tool allows you to:

  • Provide the legacy function specification,

  • generate a C++ MEX S-function that is used during simulation to call the legacy code, and

  • Compile and build the generated S-function for simulation.

Providing the Legacy Function Specification

Functions provided with the Legacy Code Tool take a specific data structure or array of structures as the argument. The data structure is initialized by calling the function legacy_code() using 'initialize' as the first input. After initializing the structure, you have to assign its properties to values corresponding to the legacy code being integrated. For detailed help on the properties, call legacy_code('help'). The definition of the legacy C++ class being used in this example is:

class adder {
      int int_state;
      int add_one(int increment);
      int get_val();

The legacy source code is found in the files adder_cpp.h and adder_cpp.cpp.

% sldemo_sfun_adder_cpp
def = legacy_code('initialize');
def.SFunctionName = 'sldemo_sfun_adder_cpp';
def.StartFcnSpec  = 'createAdder()';
def.OutputFcnSpec = 'int32 y1 = adderOutput(int32 u1)';
def.TerminateFcnSpec = 'deleteAdder()';
def.HeaderFiles   = {'adder_cpp.h'};
def.SourceFiles   = {'adder_cpp.cpp'};
def.IncPaths      = {'sldemo_lct_src'};
def.SrcPaths      = {'sldemo_lct_src'};
def.Options.language = 'C++';
def.Options.useTlcWithAccel = false;

Generating and Compiling an S-Function for Use During Simulation

The function legacy_code() is called again with the first input set to 'generate_for_sim' in order to automatically generate and compile the C-MEX S-function according to the description provided by the input argument 'def'. This S-function is used to call the legacy functions in simulation. The source code for the S-function is found in the file sldemo_sfun_adder_cpp.cpp.

legacy_code('generate_for_sim', def);
### Start Compiling sldemo_sfun_adder_cpp
    mex('-I/mathworks/devel/bat/BR2018bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src', '-I/tmp/BR2018bd_921823_138171/publish_examples0/tpa27e3108', '-c', '-outdir', '/tmp/BR2018bd_921823_138171/publish_examples0/tp70f05901_9744_4e37_82bd_c812b9fc6c0f', '/mathworks/devel/bat/BR2018bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src/adder_cpp.cpp')
Building with 'g++'.
MEX completed successfully.
    mex('sldemo_sfun_adder_cpp.cpp', '-I/mathworks/devel/bat/BR2018bd/build/matlab/toolbox/simulink/simdemos/simfeatures/sldemo_lct_src', '-I/tmp/BR2018bd_921823_138171/publish_examples0/tpa27e3108', '-cxx', '/tmp/BR2018bd_921823_138171/publish_examples0/tp70f05901_9744_4e37_82bd_c812b9fc6c0f/adder_cpp.o')
Building with 'g++'.
MEX completed successfully.
### Finish Compiling sldemo_sfun_adder_cpp
### Exit

Generating an rtwmakecfg.m File for Code Generation

After the TLC block file is created, the function legacy_code() can be called again with the first input set to 'rtwmakecfg_generate' in order to generate an rtwmakecfg.m file to support code generation through Simulink® Coder™. Generate the rtwmakecfg.m file if the required source and header files for the S-functions are not in the same directory as the S-functions, and you want to add these dependencies in the makefile produced during code generation.

Note: Complete this step only if you are going to simulate the model in accelerated mode.

legacy_code('rtwmakecfg_generate', def);

Generating a Masked S-Function Block for Calling the Generated S-Function

After the C-MEX S-function source is compiled, the function legacy_code() can be called again with the first input set to 'slblock_generate' in order to generate a masked S-function block that is configured to call that S-function. The block is placed in a new model and can be copied to an existing model.

% legacy_code('slblock_generate', def);

Demoing the Generated Integration with Legacy Code

The model sldemo_lct_cpp shows integration with the legacy code.