Integrate External C++ Object Methods
Integrate legacy C++ object methods by using the Legacy Code Tool.
With the Legacy Code Tool, you can:
Provide the legacy function specification.
Generate a C++ MEX S-function that calls the legacy code during simulation.
Compile and build the generated S-function for simulation.
Generate a block TLC file and optional
rtwmakecfg.m
file that calls the legacy code during code generation.
Provide 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, assign its properties to values corresponding to the legacy code being integrated. The definition of the legacy C++ class in this example is:
class adder { private: int int_state; public: adder(); int add_one(int increment); int get_val(); };
The legacy source code is in the files adder_cpp.h
and adder_cpp.cpp
.
% sfun_adder_cpp def = legacy_code('initialize'); def.SFunctionName = '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 = {'Lct_src'}; def.SrcPaths = {'Lct_src'}; def.Options.language = 'C++'; def.Options.useTlcWithAccel = false;
Generate an S-Function for Simulation
To generate a C-MEX S-function according to the description provided by the input argument def
, call the function legacy_code()
again with the first input set to sfcn_cmex_generate
. The S-function calls the legacy functions during simulation. The source code for the S-function is in the file sfun_adder_cpp.cpp
.
legacy_code('sfcn_cmex_generate', def);
Compile the Generated S-Function for Simulation
After you generate the C-MEX S-function source file, to compile the S-function for simulation with Simulink®, call the function legacy_code()
again with the first input set to compile
.
legacy_code('compile', def);
### Start Compiling sfun_adder_cpp mex('-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671/Lct_src', '-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671', '-c', '-outdir', '/tmp/Bdoc24b_2725827_4048235/tp92af3f10_a09b_4d93_b2e8_3165c419dd33', '/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671/adder_cpp.cpp') Building with 'g++'. MEX completed successfully. mex('sfun_adder_cpp.cpp', '-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671/Lct_src', '-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671', '-cxx', '/tmp/Bdoc24b_2725827_4048235/tp92af3f10_a09b_4d93_b2e8_3165c419dd33/adder_cpp.o') Building with 'g++'. MEX completed successfully. ### Finish Compiling sfun_adder_cpp ### Exit
Generate a TLC Block File for Code Generation
After you compile the S-function and use it in simulation, you can call the function legacy_code()
again. Set the first input to sfcn_tlc_generate
to generate a TLC block file that supports code generation through Simulink® Coder™. If the TLC block file is not created and you try to generate code for a model that includes the S-function, code generation fails. The TLC block file for the S-function is sfun_adder_cpp.tlc
.
legacy_code('sfcn_tlc_generate', def);
Generate an rtwmakecfg.m File for Code Generation
After you create the TLC block file, you can call the function legacy_code()
again. Set the first input to rtwmakecfg_generate
to generate an rtwmakecfg.m file that supports code generation through Simulink® Coder™. If the required source and header files for the S-function are not in the same folder as the S-function, and you want to add these dependencies in the makefile produced during code generation, generate the rtwmakecfg.m
file.
legacy_code('rtwmakecfg_generate', def);
Generate Masked S-Function Block for Calling the Generated S-Function
After you compile the C-MEX S-function source, you can call the function legacy_code()
again. Set the first input to slblock_generate
to generate a masked S-function block that is configured to call that S-function. The software places the block in a new model. You can copy the block to an existing model.
legacy_code('slblock_generate', def);
Show the Generated Integration with Legacy Code
The model LctExternalCodeImportCpp
shows integration with the legacy code.
open_system('LctExternalCodeImportCpp') sim('LctExternalCodeImportCpp')
ans = ' ### Start Compiling sfun_adder_cpp mex('-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671/Lct_src', '-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671', '-c', '-outdir', '/tmp/Bdoc24b_2725827_4048235/tpa89cbee0_a040_4783_a0b8_35ee6a93b8eb', '/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671/adder_cpp.cpp') Building with 'g++'. MEX completed successfully. mex('sfun_adder_cpp.cpp', '-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671/Lct_src', '-I/tmp/Bdoc24b_2725827_4048235/tp88aafc20/simulinkcoder-ex06428671', '-cxx', '/tmp/Bdoc24b_2725827_4048235/tpa89cbee0_a040_4783_a0b8_35ee6a93b8eb/adder_cpp.o') Building with 'g++'. MEX completed successfully. ### Finish Compiling sfun_adder_cpp ### Exit '