Interface to a Development Computer Simulator by Using a Shared Library
This example generates a shared library for interfacing to a simulator that runs on your development computer. Generate the shared library by using the system target file ert_shrlib.tlc
.
To build a shared library from the model and use the library in an application:
1. Develop your model. For this example, open the model SharedLibraryCode
. Save a copy of the model to a writable location.
open_system('SharedLibraryCode');
The model is a single-rate, discrete-time model. An 8-bit counter feeds the triggered subsystem named Amplifier
. Parameters INC
, LIMIT
, and RESET
are set to constant values 1, 4, and 0, respectively. When signal equal_to_count
is true, the subsystem amplifies its input signal by a gain factor K=3
and the output signal is updated.
2. Open the Embedded Coder app.
3. Set model configuration parameter System target file to ert_shrlib.tlc
.
4. Build the shared library file. The file that the code generator produces depends on your development platform. For example, on a Windows® system, the code generator produces the library file SharedLibraryCode_win64.dll
.
5. Create application code that uses the shared library. This example uses application code that is available in these files:
SharedLibraryCode_app.h
SharedLibraryCode_app.c
To view the source code in these files, in the model, click the white buttons for the .h
and .c
files.
6. Compile and link the file application and shared library files to produce an executable program. The following script compiles, builds, and runs the program.
run_SharedLibraryCode_app
=== Demo usage of ERT Shared Library Target === === Parameter List === LIMIT: 4; INC: 1; RESET: 0; K: 3 === Build SharedLibraryCode === ### Starting build procedure for: SharedLibraryCode ### Successful completion of build procedure for: SharedLibraryCode Build Summary Top model targets: Model Build Reason Status Build Duration ==================================================================================================================== SharedLibraryCode Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 8.8118s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 9.3269s Building with 'gcc'. MEX completed successfully. === Execute example application === Counter out(sum_out): 1 Amplifier in(Input): 0 out(Output): 0 Counter out(sum_out): 2 Amplifier in(Input): 1 out(Output): 0 Counter out(sum_out): 3 Amplifier in(Input): 2 out(Output): 0 Counter out(sum_out): 4 Amplifier in(Input): 3 out(Output): 0 Counter out(sum_out): 1 Amplifier in(Input): 4 out(Output): 12 Counter out(sum_out): 2 Amplifier in(Input): 5 out(Output): 12 Counter out(sum_out): 3 Amplifier in(Input): 6 out(Output): 12 Counter out(sum_out): 4 Amplifier in(Input): 7 out(Output): 12 Counter out(sum_out): 1 Amplifier in(Input): 8 out(Output): 24 Counter out(sum_out): 2 Amplifier in(Input): 9 out(Output): 24 Counter out(sum_out): 3 Amplifier in(Input): 10 out(Output): 24 Counter out(sum_out): 4 Amplifier in(Input): 11 out(Output): 24 Counter out(sum_out): 1 Amplifier in(Input): 12 out(Output): 36 === End of Demo ===
To view the script code, in the model, click the white button for the .m
file.
To build the model and run the application that uses the generated shared library, in the model, double-click the blue button.
For more information about using a shared library, see Package Generated Code as Shared Libraries.