How can I generate an S-function that calls a C++ static library with other library dependencies?

8 views (last 30 days)
I would like to see an example where I can call a static library from an S-function used in a Simulink model. The .lib file is generated from a C++ file and has calls to other libraries as well. How do I specify the legacy code tool commands to generate this S-function?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Jul 2018
S-functions that call legacy static C++ libraries containing other library dependencies can be generated using the legacy code tool. Here is an example that generates an S-function my_lib_adder_cpp that calls a function from a static library file Project_Eg.Lib. This library in turn calls functions from another static library mult_cpp.lib. The generated S-function is called from the Simulink model my_sldemo_lct_cpp.mdl. The S-function simply accumulates the input and scales it by 5. The following steps illustrate how the libraries were generated from a C++ source file using Visual Studio and integrated into the S-function.
1. The example contains a source cpp and header file mult_cpp.c and mult_cpp.h. mult_cpp.c creates a class instance Multipliers with its own constructor and destructor. The function multOutput() in this file simply multiplies an input by 5.This source file is then compiled into a library mult_cpp.lib in Visual Studio using the steps given here:
<http://msdn.microsoft.com/en-us/library/ms235627(VS.80).aspx>
To do this using Visual Studio command line options, launch the Visual Studio command prompt (Start => Programs => Visual C++ => Visual Studio Tools => Visual Studio Command prompt) and follow the steps given below:
a. At the prompt navigate to the directory where the source and header file reside.
b.Create an object file using the cl command and the -c option
>> cl -c mult_cpp.cpp
c. Use LIB command to compile the generated object file mult_cpp.obj into a static library. The /OUT option is to override the default output file name.
>> LIB mult_cpp.obj /OUT:mult_cpp.lib
2. An additional source file adder_cpp.cpp contains code to accept an input, increment it by the specified increment value and call multOutput() to scale it. In a separate project in Visual Studio, this file (with its header mult_cpp.h) is again compiled into another library Project_Eg.lib using the steps listed above.
3.Ensure that the compiler option in MATLAB is set to Visual C++ using
mex -setup
4. The following legacy code tool scipt is used to create an S-function from the two libraries created above:
def = legacy_code('initialize');
def.SFunctionName = 'my_lib_adder_cpp';
def.OutputFcnSpec = 'int32 y1 = adderOutput(int32 u1)';
def.StartFcnSpec = 'createAdder()';
def.TerminateFcnSpec = 'deleteAdder()';
def.HeaderFiles = {'adder_cpp.h'};
def.HostLibFiles = {'Project_Eg.Lib', 'Mult_lib.lib'};
def.IncPaths = Header_Path;
def.LibPaths = Lib_Path;
def.Options.useTlcWithAccel = false;
def.Options.language = 'C++';
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def);
where Header_Path and Lib_Path are set to where the header and library files reside. Notice that def.HostLibFiles specified both the library files Project_Eg.Lib and its dependency Mult_lib.lib. This is because during the Visual Studio compilation process, Project_Eg.Lib may not have the references to Mult_lib.lib resolved.This can be confirmed by listing the symbols in the library file Project_Eg.Lib using the Windows dumpbin utility. Using this command you can list out the symbol table for Project_Eg.Lib. This table has all the function calls listed out. For functions that are contained in the library, the symbols show up in well-defined sections (given in the SECT column). But external function calls such as calls to multoutput() here appear as UNDEF meaning that additional library files need to be provided for this function call. Hence functions that belonged to Mult_lib.lib are listed out as UNDEF in the Symbol table.
On a Windows machine with Visual Studio IDE installed, you can use the dumpbin utility from the Visual Studio Command prompt . Use the following command to list out the symbols in Project_Eg.Lib
dumpbin \SYMBOLS Project_Eg.Lib
Note that the attached model was created on a 32-bit machine. The already-generated S-Function included in this folder will not work on 64-bit machines.
The symbol table of the above file appears as:
COFF SYMBOL TABLE
052 00000000 SECT13 notype () External | ?adderOutput@@YAHH@Z (int __cdecl adderOutput(int))
053 00000000 UNDEF notype () External | ?deleteMult@@YAXXZ (void __cdecl deleteMult(void))
054 00000000 UNDEF notype () External | ?multOutput@@YAHH@Z (int __cdecl multOutput(int))
055 00000000 UNDEF notype () External | ?createMult@@YAXXZ (void __cdecl createMult(void))
The generated S-function my_lib_adder_cpp is called from the model my_sldemo_lct_cpp. All the files required and generated in this example are provided in the resolution documents in Project_lib_Eg.zip.
  1 Comment
Julia Antoniou
Julia Antoniou on 5 Jul 2018
It looks like the S-Function in this example was created on a 32-bit version of Windows in an older release of MATLAB. The MEX file associated with this S-Function has the extension "mexw32". "mexw32" files will only work for 32-bit Windows versions of MATLAB. This MATLAB answers post has a good explanation of why 32-bit MEX files cannot be used on 64-bit.
You could try following the steps outlined above to create the same S-Function that is compatible with your 64-bit machine.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Tags

Products


Release

R2009a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!