How to generate a shared library (SO/DLL) using CMake-based toolchain in Simulink
22 views (last 30 days)
Show older comments
Hello everyone,
I have defined a CMake-based toolchain in MATLAB 2024b as follows:
% matlab script
tc = target.create('Toolchain', 'Name', 'MySimpleToolchain');
tc.Builder = target.create('CMakeBuilder');
tc.Builder.Generator = 'Ninja';
tc.Builder.ToolchainFile = fullfile(pwd, '\LinuxRT_Toolchain\LinuxRT.cmake');
tc.Builder.SupportedBuildTypes(end+1) = ...
target.create('CMakeBuildType', ...
'Name', 'FastMath', ...
'GeneratesDebugSymbols', false, ...
'DebugBuildType', 'FastMathWithDebug');
tc.Builder.SupportedBuildTypes(end+1) = ...
target.create('CMakeBuildType', ...
'Name', 'FastMathWithDebug', ...
'GeneratesDebugSymbols', true);
tc.EnvironmentConfiguration(1).HostOperatingSystemSupport.Linux = true;
tc.EnvironmentConfiguration(1).HostOperatingSystemSupport.Windows = false;
tc.EnvironmentConfiguration(1).HostOperatingSystemSupport.Mac = false;
tc.EnvironmentConfiguration(1).SystemPaths = ...
{'$(MW_MINGW64_LOC)/bin', 'C:/Ninja'};
tc.EnvironmentConfiguration(2) = target.create('EnvironmentConfiguration');
tc.EnvironmentConfiguration(2).HostOperatingSystemSupport.Linux = false;
tc.EnvironmentConfiguration(2).HostOperatingSystemSupport.Windows = true;
tc.EnvironmentConfiguration(2).HostOperatingSystemSupport.Mac = false;
tc.EnvironmentConfiguration(2).SystemPaths = ...
{'C:/Ninja'};
windowsProc = target.get('Processor', 'Intel-x86-64 (Windows64)');
tc.SupportedHardware(end+1) = ...
target.create('HardwareComponentSupport', 'Component', windowsProc);
linuxProc = target.get('Processor', 'Intel-x86-64 (Linux 64)');
tc.SupportedHardware(end+1) = ...
target.create('HardwareComponentSupport', 'Component', linuxProc);
macProc = target.get('Processor', 'Intel-x86-64 (Mac OS X)');
tc.SupportedHardware(end+1) = ...
target.create('HardwareComponentSupport', 'Component', macProc);
target.add(tc);
I use this toolchain with grt.tlc to generate an executable from a Simulink model — this works smoothly.
Now, I would like to generate a shared library (.so or .dll, depending on the platform) from the model instead of an executable. But the defined CMakelists.txt file has the following statements:
...
# Definition of target "samplemodel"
add_executable(samplemodel ${MATLAB_ROOT}/rtw/c/src/common/rt_main.c)
...
My questions are:
- Should I simply add certain build options to the existing toolchain to change add_executable(...) statement with add_library(....)?
- Or is it necessary to create a custom TLC file instead of using grt.tlc?
Thanks in advance for your help.
0 Comments
Answers (1)
Aabha
on 18 Aug 2025 at 7:01
I understand that you wish to generate a shared library from your model code instead of an executable. In this case, adding certain specific build options to the existing toolchain may not be useful, since MATLAB does not change the structure of the generated ‘CMakeLists’ file based on the additional build options alone. The documentation on configuring the ‘CMake’ build process clarifies what build types and customization options are available when using ‘CMake’, as follows:
The code generation process produces 'CMakeLists.txt', which drives the build. However, changing the output type (executable vs. library) is controlled in Simulink via the system target file and build settings, not via 'CMake' flags alone.
As an alternative, you can modify the generated ‘CMakeLists’ file to include the ‘add_library’ option, by using a flag ‘BUILD_SHARED_LIB’ as follows:
if(BUILD_SHARED_LIB)
add_library(modelname SHARED ${MODEL_SOURCES})
else()
add_executable(modelname ${MODEL_SOURCES} ${RT_MAIN})
end
Then, you can pass the flag using the additional build options as follows:
tc.Builder.AdditionalBuildOptions = {'-DBUILD_SHARED_LIB=ON'};
After this, when you build, MATLAB will generate the normal ‘CMakeLists.txt’, but also include the additional flag in the ‘cmake’ command line.
Alternatively, you can build a shared library by configuring the code generator to use the system target file ‘ert_shrlib.tlc’. This method does not require any modifications in the existing toolchain and will generate a shared library version of your model code. Also, it does not change the code that the code generator produces for your model. Please refer to the following documentation link for more information regarding the same:
I hope this helps.
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!