, !
, Simulink. ? , -, « Simulink? DLL?» - .
Simulink.
, .
, Simulink.
, Simulink exlib.
exlib.c exlib.h. , :
void exlib_init(void) – .
void exlib_print(float data) – .
void exlib_term(void) – .
.
, . 3, , .dll ( .so Linux) (.h) . Windows .lib, , . .
, MATLAB mex, (exlib.dll Windows exlib.so Linux). ,
mex -setup
.
mex :
mingw = strfind(mex.getCompilerConfigurations('C','Selected').Name,'MinGW64 Compiler');
if isunix
mex('LDEXT=.so','LINKEXPORT=','LINKEXPORTVER=','LINKLIBS=','exlib.c');
elseif mingw
mex('LDEXT=.lib','LINKEXPORT=','LINKEXPORTVER=','LINKLIBS=','exlib.c');
mex('LDEXT=.dll','LINKEXPORT=','LINKEXPORTVER=','LINKLIBS=','exlib.c');
else
mex('LDEXT=.dll','LINKEXPORT=','LINKEXPORTVER=','CMDLINE300="del exlib.exp exlib.dll.manifest"','exlib.c');
end
Building with 'gcc'.
MEX completed successfully.
, MATLAB:
[~,~] = loadlibrary(['exlib',system_dependent('GetSharedLibExt')],'exlib.h');
libfunctionsview('exlib');
calllib('exlib','exlib_init');
for i = 1:10
calllib('exlib','exlib_print',single(i));
end
calllib('exlib','exlib_term');
unloadlibrary('exlib');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
, , , Simulink!
S-
S- C- Simulink. C, . , , Simulink . S-. Simulink S-. S- Simulink.
Legacy Code Tool
Legacy Code Tool, , S- , MATLAB. S- ( Windows * .lib) . .
, Legacy Code Tool
Legacy Code Toolspecs = legacy_code('initialize');
specs.StartFcnSpec = 'exlib_init()';
specs.OutputFcnSpec = 'exlib_print(single u1)';
specs.TerminateFcnSpec = 'exlib_term()';
specs.HostLibFiles = {['exlib',strrep(system_dependent('GetSharedLibExt'),'.dll','.lib')]};
specs.HeaderFiles = {'exlib.h'};
specs.SFunctionName = 'sfun_exlib';
S-, :
legacy_code('generate_for_sim',specs);
### Start Compiling sfun_exlib
mex('sfun_exlib.c', '-I/tmp/simulink_shrlib_fex', '/tmp/simulink_shrlib_fex/exlib.so')
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling sfun_exlib
### Exit
, Simulink:
legacy_code('slblock_generate',specs);
:
open_system('simlib_test');
snapnow;
sim('simlib_test');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
S-
S- . , . S-, Legacy Code Tool.
mdlStart,
mdlOutputs mdlTerminate.
:
void mdlStart(SimStruct *S)
{
#if defined(__GNUC__) && !defined(__MINGW32__)
void (*exlib_init_ptr)(void);
dllHandle = dlopen("./exlib.so",RTLD_LAZY);
exlib_init_ptr = dlsym(dllHandle, "exlib_init");
#else
exlib_init_type exlib_init_ptr = NULL;
dllHandle = LoadLibrary("exlib.dll");
exlib_init_ptr = (exlib_init_type)GetProcAddress(dllHandle,"exlib_init");
#endif
exlib_init_ptr();
}
void mdlOutputs(SimStruct *S, int_T tid)
{
real32_T *u1 = 0;
#if defined(__GNUC__) && !defined(__MINGW32__)
void (*exlib_print_ptr)(float);
exlib_print_ptr = dlsym(dllHandle,"exlib_print");
#else
exlib_print_type exlib_print_ptr = NULL;
exlib_print_ptr = (exlib_print_type)GetProcAddress(dllHandle,"exlib_print");
#endif
u1 = (real32_T *) ssGetInputPortSignal(S, 0);
exlib_print_ptr( *u1);
}
void mdlTerminate(SimStruct *S)
{
#if defined(__GNUC__) && !defined(__MINGW32__)
void (*exlib_term_ptr)(void);
exlib_term_ptr = dlsym(dllHandle,"exlib_term");
exlib_term_ptr();
dlclose(dllHandle);
#else
exlib_term_type exlib_term_ptr = NULL;
exlib_term_ptr = (exlib_term_type)GetProcAddress(dllHandle,"exlib_term");
exlib_term_ptr();
FreeLibrary(dllHandle);
#endif
}
S-:
if isunix
mex('sfun_exlib_dyn.c','-ldl');
else
mex('sfun_exlib_dyn.c');
end
Building with 'gcc'.
MEX completed successfully.
:
open_system('simlib_test_dyn');
sim('simlib_test_dyn');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
S-Function Builder
S-Function Builder. , Legacy Code Tool S- . S-Function Builder , S-, S- .
S-Function Builder . S-. C function, R2020 .
MATLAB Function
MATLAB Function MATLAB Simulink.
MATLAB
coder.ceval, MATLAB Function ( MATLAB). coder.ceval MATLAB Coder.
MATLAB Function, :
function fcn(u)
persistent runTimeCnt
coder.extrinsic('pwd','system_dependent');
libpath = coder.const(pwd);
libname = coder.const(['exlib',strrep(system_dependent('GetSharedLibExt'),'.dll','.lib')]);
coder.updateBuildInfo('addLinkObjects',libname,libpath,1000,true,true);
coder.updateBuildInfo('addIncludePaths',libpath);
coder.cinclude('exlib.h');
if isempty(runTimeCnt)
coder.ceval('exlib_init');
runTimeCnt = 0;
end
coder.ceval('exlib_print',single(u));
runTimeCnt = runTimeCnt+1;
if (runTimeCnt == 11)
coder.ceval('exlib_term');
end
– ( MATLAB System).
, exlib_term ();
Simulation Target -> Custom Code -> Terminate function.
. «Import custom code», «Simulation Target» ( coder.cinclude coder.updateBuildInfo). , Simulation Target, coder.cinclude coder.updateBuildInfo.
— ,
coder.cinclude coder.updateBuildInfo,
exlib_term() , .
:
open_system('simlib_test_mlf');
sim('simlib_test_mlf');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
Stateflow
Stateflow, Stateflow. Stateflow , , .
Stateflow – Stateflow:

, , Stateflow , .
Simulation Target -> Custom Code -> Libraries exlib.lib ( exlib.so Linux).
Simulation Target -> Custom Code -> Header File #include «exlib.h». .
Simulation Target -> Custom Code -> Terminate function exlib_term() ;.
:
if isunix
set_param('simlib_test_sf','SimUserLibraries','exlib.so');
end
sim('simlib_test_sf');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
, Stateflow,
C. , Stateflow
MATLAB,
coder.ceval, MATLAB Function.
MATLAB System
MATLAB System Simulink. .
Simulink R2013b. , , . MATLAB — , - — C.
, MATLAB System:
classdef exlib < matlab.System
properties (Nontunable,Access=private)
libName = exlib.getLibName;
libPath = pwd;
libHeader = 'exlib.h';
end
methods (Static)
function libName = getLibName
if isunix
libName = 'exlib.so';
else
libName = 'exlib.lib';
end
end
end
methods (Access=protected)
function setupImpl(obj, ~)
coder.updateBuildInfo('addLinkObjects',obj.libName,obj.libPath,1000,true,true);
coder.updateBuildInfo('addIncludePaths',obj.libPath);
coder.cinclude(obj.libHeader);
coder.ceval('exlib_init');
end
function stepImpl(~, u)
coder.ceval('exlib_print',u);
end
function releaseImpl(~)
coder.ceval('exlib_term');
end
end
end
:
open_system('simlib_test_mls');
sim('simlib_test_mls');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
C Caller
C Caller C Simulink. .
, MATLAB R2018b. — Simulink C . , .
,
exlib.so/exlib.lib Simulation Target -> Libraries #include «exlib.h» Simulation Target -> Header , «Refresh custom code» C Caller, , .
exlib_print, :

,
exlib_init exlib_term, Simulation Target. C Caller . C Caller Initialize Function Terminate Function. Stateflow:
Schedule Subsystems to Execute at Specific Times:
open_system('simlib_test_ccaller');
if isunix
set_param('simlib_test_ccaller','SimUserLibraries','exlib.so');
end
sim('simlib_test_ccaller');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
C Function
Simulink
C Function. R2020a.
C Caller , - C ( , S-Function Builder). , , C Function C, C, . , .
exlib.so/exlib.lib «Simulation Target -> Libraries» #include «exlib.h» «Simulation Target -> Header file» .
C Function single , :

open_system('simlib_test_cfunction');
if isunix
set_param('simlib_test_cfunction','SimUserLibraries','exlib.so');
end
sim('simlib_test_cfunction');
type('exlib.txt');
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
Embedded Coder
Embedded Coder C- Simulink . , , , C.
, C Simulink, S- Simulink Coder
«--» (Software-in-the-Loop). , Embedded Coder , , , .
, Embedded Coder, , . , :
open_system('simlib_test_ert');
snapnow;

.dll (.so Linux), .lib ( .dll) .exp ( .dll).
if isunix
set_param('simlib_test_ert','CustomHeaderCode','#include <stddef.h>');
end
rtwbuild('simlib_test_ert');
### Starting build procedure for: simlib_test_ert
### Successful completion of build procedure for: simlib_test_ert
:
ex_init
double ex_step(double, double)
simlib_test_ert_terminate
, , . , ( ).
Out1 = ex_step (In1, In2).
. , MATLAB Function ( ):
function y = fcn(u1, u2)
coder.extrinsic('RTW.getBuildDir','fullfile');
buildDir = coder.const(RTW.getBuildDir('simlib_test_ert'));
libpath = coder.const(buildDir.CodeGenFolder);
incpath = coder.const(fullfile(buildDir.BuildDirectory,'simlib_test_ert.h'));
if isunix
ext = '.so';
libname = ['simlib_test_ert',ext];
else
ext = '.lib';
libname = ['simlib_test_ert_',computer('arch'),ext];
end
coder.updateBuildInfo('addLinkObjects',libname,libpath,1000,true,true);
coder.updateBuildInfo('addIncludePaths',libpath);
coder.cinclude(incpath);
y = 0;
y = coder.ceval('ex_step',u1,u2);
:
open_system('simlib_test_callert');
sim('simlib_test_callert');
snapnow;

Simulink. , . , , .
Legacy Code Tool , , .
MATLAB System — , :
- , , ,
- MATLAB MATLAB
- MATLAB System (, S-)
MATLAB Function , . , Legacy Code Tool S- - .
S- . ,
LoadLibrary/dlopen,
GetProcAddress/dlsym,
FreeLibrary/dlclose, .
C Caller, R2018b, , C, .
C Function, R2020a.