MATLAB Answers

How Can I Generate a 32-bit DLL from a Simulink Model on 64-bit MATLAB?

47 views (last 30 days)
I am using a 64-bit MATLAB installation to develop Simulink models on a 64-bit Windows machine, and had been able to build 64-bit DLLs from these models using Visual Studio compilers.
Now, I would like to build those same models as 32-bit DLLs. How could I be able to achieve this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Sep 2021
Edited: MathWorks Support Team on 22 Sep 2021
The best way to set this up is by registering a custom toolchain for your build process. A toolchain is simply a collection of tools used to build the code that gets generated from one of our coder products (in this case Embedded Coder generating code from a Simulink model). Now generally speaking custom toolchain registration can be somewhat involved as there is a lot of flexibility in this method and specifying all of the different aspects can be time consuming. Fortunately we already have an example that registers a toolchain for building a 32 bit DLL on a 64 bit windows machine using Visual Studio! You can find the example at the following documentation page:
The example itself is in the form of a script that registers the toolchain for use with MATLAB Coder, but all toolchains can be used across the Coder products so it should be suitable for our purposes as well. 
To start, follow the example up until the section titled "Create Code Generation Configuration Object". This means the last command you should have run should be "RTW.TargetRegistry.getInstance('reset');" We don't want to run the whole example because they remove the new toolchain in the clean up at the end of the example. Notice that there's a 'tc' variable of class Toolchaininfo created in the base workspace.
Then we need to open the Simulink model and make a few modifications to ensure that we are setup properly to use the new toolchain. First we modify the Hardware implementation to specify that we are targeting a 32-bit Windows computer as seen below:
Next we need to ensure that we are using "ert_shrlib.tlc" to generate our shared library.
Save the model, close the configuration parameter pane and reopen it. Then, all we need to do is choose our new toolchain from the list of toolchains:
And then we should be all set to generate a 32 bit DLL.
For more information about custom toolchains, you can look at the following documentation page detailing the various steps of custom toolchain creation and registration:
Some potential troubleshooting steps:
Sometimes after setting the hardware the toolchain still does not appear under the dropdown. To resolve this make sure that the hardware settings have been applied, and close the configuration parameters. Reopen the configuration parameters and you should see the toolchain appear as an option. Additionally saving the model after applying the hardware change can allow the new toolchain to be found.
Rasmus Visgaard
Rasmus Visgaard on 16 Mar 2021
I'm geeting the error "Undefined function or variable 'my_msvc_32bit_tc'." while trying to follow the steps in
I'm running on Matlab 2017a. Does anyone have a solution to that?

Sign in to comment.

More Answers (1)

Ricardo Gutierrez
Ricardo Gutierrez on 23 Nov 2020
I'm trying to run the example you provided, however, it runs and creates the "CODEGEN" folder, however it doesn't create the .dll file. Can you help me please? I need to run the dll in 32-bit software. Thanks for listening.


No tags entered yet.




Community Treasure Hunt

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

Start Hunting!