MATLAB: Resolve ” relocation truncated to fit” errors by dynamically allocating large non variable sized arrays with Simulink Coder

Embedded Codermatlab codersimulink coder

I have a Simulink model with a MATLAB function block that operates on a very large array. When I build my model and try to compile an executable with the MinGW compiler, I get errors like that shown below:
modelName.obj:modelName.c:(.text+0x24): relocation truncated to fit: R_X86_64_PC32 against symbol ‘modelName_DW’ defined in COMMON section in modelName.obj

Best Answer

This error occurs when the size of the code and the statically allocated data exceeds the size that a 32 bit relative addressing scheme can reach. See the post below for more information.
This can occur if you have a very large array in a MATLAB function block that is not variable sized. The array will be statically allocated in the generated code and thus the memory consumption will be too great for the linker to handle. See the attached model "largeStaticArrayError" for an example. This model will reproduce the error when you press the build button.
Currently there is no way in Simulink Coder/MATLAB Coder to dynamically allocate arrays that are not variable sized. To resolve this issue, try one of the following solutions:
1) Set the Configuration Parameter in the link below to "Reusable Function". If you are using ert.tlc be sure to turn on "Use dynamic memory allocation for model initialization". Part of what this will do is to dynamically allocate memory for internal states and data.
2) Make the array appear to be variable-sized even if you do not intend to change the size of the array. To do this, make the size of the array depend on an input parameter to the MATLAB function block. Define this parameter in the MATLAB workspace. Then turn on "dynamic memory allocation for variable sized arrays" in the configuration parameters. See that attached model "VariableSizedArrayWorkaround" as an example.