How can I generate a 32-bit SO for a Simulink model?

14 views (last 30 days)
How can I generate a 32-bit SO file with a supported GCC compiler for a Simulink model? I have followed this example to generate a 32-bit DLL on Windows, but I couldn't find an example for Linux environment:

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Mar 2025
Edited: MathWorks Support Team on 14 Mar 2025
To build a 32-bit SO file, you need to choose a device type with the length of "long" as 32 bits.  One option in this instance would be to choose:
Device vendor:  Generic
Device type:  32-bit x86 compatible
If you want to customize further, there is the target namespace, which allows the definition of custom device types.  See https://mathworks.com/help/coder/ug/register-new-hardware-devices.html#mw_cea14c66-5050-43a4-a8b4-d2d175b7f64b for more details on how to do this.
If you are using a 64-bit Linux host:
You can generate a 32-bit shared library (.SO) on 64-bit Linux using 'ert_shrlib.tlc' and the ' -m32' toolchain option.
In Configuration Parameters, set 'System target file', 'Toolchain', and 'Build configuration' as follows and add '-m32' to 'C Compiler' and 'Shared Library Linker' options:
Alternatively, the following code is an example script for setting a toolchain and options programmatically: 
model = 'modelName'; % set TLC for shared library generation set_param(model, 'SystemTargetFile', 'ert_shrlib.tlc'); % set toolchain set_param(model, 'Toolchain', 'GNU gcc/g++ | gmake (64-bit Linux)'); set_param(model, 'BuildConfiguration', 'Specify'); % get default toolchain options opts = get_param(model, 'CustomToolchainOptions'); opts{2} = [opts{2} ' -m32']; % add -m32 to C Compiler options opts{6} = [opts{6} ' -m32']; % add -m32 to Shared Library Linker options % set custom toolchain options set_param(model, 'CustomToolchainOptions', opts); 
In this example script, 'opts' is a cell array with compile options that can be customized.
 
If you use a version after R2023b, you can also create your custom toolchain using the "target.Toolchain" objects. 
To create it, you can run the following code:
toolchain = target.create('Toolchain', ... 'Name', 'GCC for 32-bit Linux', ... 'Family', 'GNU', ... 'CCompiler', 'gcc -m32', ... 'CppCompiler', 'g++ -m32', ... 'ExecutableExtension', '', ... 'SharedLibraryExtension', '.so'); ld = toolchain.Tools(strcmp({toolchain.Tools.Name}, 'GCC for 32-bit Linux - Linker')); ld.setDirective('Shared', '-shared'); cppld = toolchain.Tools(strcmp({toolchain.Tools.Name}, 'GCC for 32-bit Linux - C++ Linker')); cppld.setDirective('Shared', '-shared'); target.add(toolchain);
You can then set it using:
% set toolchain set_param(model, 'Toolchain', 'GCC for 32-bit Linux');
If you are using a Windows host:
From  R2024a onwards, it's possible to define a toolchain that builds on a remote Linux host from a Windows host by transferring the source code via SSH and then running CMake on the remote Linux host. 
You would have to customize the toolchain appropriately to build the 32-bit binary. 

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!