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?

답변 (1개)

Vandit
Vandit 2023년 5월 31일
편집: Stefanie Schwarz 2025년 11월 18일

0 개 추천

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

Paul
Paul 2023년 6월 20일
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 2023년 10월 26일
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deployment, Integration, and Supported Hardware에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 5월 25일

편집:

2025년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by