how do I override rt_main in embedded coder?

Using embedded coder to generate code, I want to exclude rt_main.c from the build and use my own customized version (since the one that gets included does not actually run any of my code). I see that I can generate an example main program (ert_main.c) but I don't see how I can incorporate my customized version of that file into the build. I can add my own main function to the build, but then I get a linker error because main is defined twice. How do I write a customized main function?

Réponses (1)

Vandit
Vandit le 31 Mai 2023
Modifié(e) : Stefanie Schwarz le 18 Nov 2025

0 votes

Hi,
To modify main function ‘ert_main.c’, you can navigate to ‘Model_Name_ert_rtw’ folder and customize ‘ert_main.c’ file as per your requirements.
Now to generate a new executable, please refer to the steps shown below:
1. Inside the ‘Model_Name_ert_rtw’ folder, delete all ‘.obj’ files.
2. Delete the already existing ‘Model_Name.exe’ file from your current working directory.
3.Run the ‘Model_Name.bat’ file by running the command ‘!Model_Name.bat’ in the command window to generate the executable again.
Hope this helps.

3 commentaires

Paul
Paul le 20 Juin 2023
Hi, thanks for answering.
I was looking for an automated way to do it. Obviously I can edit the generated ert_main.c and re-run the build. I don't want to do that every time I build using simulink.
What I ended up doing was turning off "generate an example main program" and in the ert_make_rtw_hook.m, when before_make is called, I look at the buildInfo and find rt_main.c, and if it's present, replace it with my own file.
Hi Paul, I want to do the same thing. Do you have find an automatic solution?
Paul
Paul le 26 Oct 2023
Yes, something like this should work in your ert_make_rtw_hook.m
function grt_make_rtw_hook(hookMethod, ...
modelName, ...
rtwroot, ...
templateMakefile, ...
buildOpts, ...
buildArgs, ...
buildInfo) %#ok<INUSL,INUSD>
switch hookMethod
case 'before_make'
% Called after code generation is complete, and just prior to kicking
% off make process (assuming code generation only is not selected.) All
% arguments are valid at this stage.
%sldiagviewer.reportInfo(getFullFileList(buildInfo));
buildInfo.Src.Files.FileName
index = find(strcmp({buildInfo.Src.Files.FileName}, 'rt_main.c'))
if (length(index) > 0)
rt_main = buildInfo.Src.Files(index)
rt_main.FileName = 'main.c';
firstFile = buildInfo.Src.Files(1)
rt_main.Path = fileparts(firstFile.Path);
end
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Simulink Coder dans Centre d'aide et File Exchange

Produits

Version

R2021b

Question posée :

le 25 Mai 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by