MATLAB: How to compile Windows generated code on a Linux machine

cross platformcross-compilesimulink coder

How can I compile code that has been generated on a Windows maching using Simulink Coder, on a Linux machine?

Best Answer

You can follow these easy steps to compile Windows generated code on a Linux machine. This has been tested using Generic Real-Time Target (grt.tlc):
1. Navigate to the Hardware Implementation pane in the model's Configuration Parameters. There, select 'Device Vendor' to be either 'AMD' or 'Intel' and 'Device Type' to be 'x86-64 (Linux 64)'.
2. Navigate to the Code Generation pane. Under 'Build process', enable the options 'Package code and artifacts' and 'Generate Code only'. Also, provide a name in the 'Zip file name' option. This will put all the necessary files needed for compiling in the zip folder. Since only an executable is needed, we can use the Toolchain approach and keep the 'Toolchain' as 'Automatically Installed Toolchain'.
3. Generate C/C++ code from your model (CTRL+B). Then, extract the generated zip folder and navigate inside the folder with the name 'model_grt_rtw'. Here, use the following commands to convert the folder to a flat structure.
>> load buildInfo.mat
>> packNGo(buildInfo);
A new folder with the model name will be generated, which will contain all the files generated by packngo in this single folder.
4. Place this folder on a Linux path and do the following operations:
  • Open the 'defines.txt' file in the folder and append the flag '-D' before all the macros, with no spaces between the flag and the macro.
  • Then, remove the spacing between all these macros and put them in a single line.
  • Copy the names of all the source files in the folder and append them to the line with the macros.
The end result will look like this:
-DMODEL=vdp -DNUMST=2 .... vdp.c vdp_data.c classic_main.c
5. Then, from a linux machine where it needs to compiled, call GCC compiler on the above line:
gcc -DMODEL=vdp -DNUMST=2 .... vdp.c vdp_data.c classic_main.c
An executable should be generated (.out file).
The above steps are a simplification of the following document: