MATLAB: How to add additional linker flags to the generated makefile

additional_ldflagsEmbedded Coderflagslinker

I am generating C/C++ code from a Simulink model using Simulink Coder or Embedded Coder.
When I try to compile the generated code by running the .bat file, I get a linking error complaining about a conflict between LIBCMT and MSVCRT, with a suggestion to add the /NODEFAULTLIB:libname option to the linker flags.
I have found that adding this option to the ADDITIONAL_LDFLAGS field of the generated .mk file solves the issue. How can this option be automatically added to the generated makefile?

Best Answer

You can add link options to the model build information by using the "addlinkflags" function. To learn more, see the following documentation page:
To add the additional linker flags to the makefile, follow below steps:
1) Create a MATLAB function setBuildArgs.m with the following code inside:
function setBuildArgs(buildInfo)
linkFlags={'/NODEFAULTLIB:libname'};
addLinkFlags(buildInfo,linkFlags);
2) Open the model you want to generate code from, execute the following command:
>> set_param(gcs,'PostCodeGenCommand','setBuildArgs(buildInfo)');
3) Generate code and check the .mk file. The flag /NODEFAULTLIB:libname should now appear in the ADDITIONAL_LDFLAGS field.