This workflow is called "cross-compilation" where we are trying to compile code in an environment (host) to be used in another (target).
Code generation and compilation are different processes, the generated code could be platform-independent but the compilation will always be dependent on the environment. The Linux target option mentioned in the question is used for code generation instead of compilation, so it does not affect the build process.
There are a few options to complete this workflow if you are primarily working on a Windows machine, and hope to deploy generated code to run on a Linux system. They are listed as follows (for simplicity, let's say Host is the Windows machine, and Target is the Linux machine):
If MATLAB (including Simulink Coder) is installed on both Host and Target, you can follow the documentation page linked below to generate code on the Host and compile it on the Target.
If you have Windows Subsystem for Linux installed and use a MATLAB version from R2025a onwards, you can build code in Windows Subsystem for Linux using CMake. You can find more information about it in the documentation below:
If MATLAB (including Simulink Coder) is only installed on the Host, you can follow the documentation page below with some minor changes in the last section ("Use CMake Configuration Files").
The code to run CMake commands are "system" commands, so they can also be run directly from the command line in a Linux terminal. For instance, you may use the following command to build an executable once you are in the "model_grt_rtw" directory*:
%% ======configure======
/path/to/matlabroot/bin/glnxa64/cmake/bin/cmake .
%% ======compile========
/path/to/matlabroot/bin/glnxa64/cmake/bin/cmake --build .
To build a shared library instead of an executable, you can use the following command to generate "CMakeList.txt" on the Host (the last command in section "Generate Code and CMake Configuration Files"). The rest of the workflow remains the same:
codebuild(buildFolder, 'BuildMethod', 'cmake', 'BuildVariant', 'SHARED_LIBRARY');
* You may need to pass the "MATLAB_ROOT" variable in the CMake configure step as it was previously defined using a Windows path, as the following:
%% ======configure======
/path/to/matlabroot/bin/glnxa64/cmake/bin/cmake -DMATLAB_ROOT="/path/to/matlabroot" .