MATLAB: How to insert the code at the top of the main header file for an AUTOSAR target

autosarblockcodecustomheadermodelsimulinkwithout

I am integrating custom C-code in my Simulink model, and wish to insert the code at the top of the main header file. To achieve this, I tried using the "Model Header" block in Simulink from the Simulink Coder library. But, for AUTOSAR target the "Model Header" block does not work. 
So, how to insert the code at the top of the main header file for an AUTOSAR target?

Best Answer

Currently, the following are the possible ways in which we can force the file to be included:
  • You could use a compiler flag to force the file to be included. Although it varies from compiler-to-compiler, there is usually a flag available to include a header file from the build command instead of an explicit “#include”. This usually takes place before anything of the “#include” directives in the code, so it should occur before the subsystems. Given the nature of Simulink Makefile generation, this file would be included for every file that is compiled. 
  • In Simulink, compile flags can be implemented for code generation using the <https://www.mathworks.com/help/rtw/ug/customizing-post-code-generation-build-processing.html PostCodeGenCommand> and the associated <https://www.mathworks.com/help/rtw/ref/addcompileflags.html addCompile> and <https://www.mathworks.com/help/rtw/ref/addlinkflags.html addLink> methods.
  • This basically uses the "rtwmakecfg.m" to customize generated Makefiles, as discussed <https://www.mathworks.com/help/rtw/ug/use-rtwmakecfg-m-api-to-customize-generated-makefiles.html here>.
  • You could also edit your code-generation template (<http://www.mathworks.com/help/ecoder/ug/code-generation-template-cgt-files.html CGT>) file and simply hard-code a “#include” directive at the top of each file. Naturally, this would still include the file everywhere, but it would do so before everything else.
  • Lastly, you could edit your code-generation template file (<http://www.mathworks.com/help/ecoder/ug/code-generation-template-cgt-files.html CGT>) and add a new section, similar to the existing “Includes”, “Defines”, etc. sections. Furthermore, you could position this new section above the existing includes and your TLC files could inject the “#include” using something like “<https://www.mathworks.com/help/rtw/tlc/code-configuration-functions.html?searchHighlight=LibSetSourceFileSection&s_tid=doc_srchtitle#bp6sid2 LibSetSourceFileSection>". This approach involves the most work, but gives you the most control about where and when the “#include” directive actually appears.