Modular Code Generation Conflicts with Autogenerated Files

4 views (last 30 days)
Dear Matlab Community,
In my work. I have large code genation projects that I would like to break apart and build in pieces.
Trying to follow the steps listed here:
  • The idea being that you can create a code geneneration application (lets call if function_A).
  • Generate code for function_A and call it in a larger code generation application function_B.
  • Where function_A's generated code is called in function_B.
To do this. I call function_A in function_B using a wrapper with "coder.ceval"
  • I update build paths to function_A code gen source code (coder.updateBuildInfo('addIncludePaths',path);)
  • I add source files for each source file created by function_A (coder.updateBuildInfo('addSourceFiles','{file_name}.c',path);)
  • I add includes for each file created by function_A (coder.cinclude('{file_name}.h');)
I have trouble when including existing generated code into projects within the Matlab Coder App.
Specific Issue: function_A creates a files called ''rt_nonfinite.c' and 'rt_nonfinite.h', and function_B tries to also create these.
Warning: File
'{path}'
not found.
> In emcRemoveFile
In emcBuildClean
In emcBuildClean
In emlckernel
In emlckernel
In emlckernel
In emlckernel
In emlcprivate
In coder.internal.gui.codegenWrapper
In codeUnifiedProject
In codeUnifiedProject
In emlcprivate
ninja: error: build.ninja:46: multiple rules generate build/win64/rt_nonfinite.obj [-w dupbuild=err]
??? Build error: C compiler produced errors. See the Build Log for further details.
So I think this makes sense. But how do I handle this?
I know I cannot remove the includes to the rt_nonfinite.c and header files from the wrapper for function_A because when I do the build fails for that component.
I tried including the function_A path, source file, and header file in the "Custom C Code for Generated Files" section of the Coder App but this didnt seem to help.
I hoped that it would stop function_B from creating rt_nonfinite.c and .h.
Because function_B hasnt been made yet, I cannot include it in the function_A build... hmm.
I am using Matlab 2020a and am unsure how to proceed.
Any info would be appreciated. Thanks.

Answers (1)

Rashed Mohammed
Rashed Mohammed on 2 Nov 2020
Hi Brian,
The following is a sample example on how to include generated code and modularize large projects.
Folder Structure:
D/
- add.m
- add3.m
- add4.m
- codegen/lib/
  • add/
  • add3/
  • add4/
Code:
add.m
function z = add(x,y)
z = x+y;
end
add3.m
function w = add3(x,y,z)
w = 0;
if coder.target('MATLAB')
w = x+y+z;
else
path = 'D:/codegen/lib/add';
coder.updateBuildInfo('addIncludePaths',path);
coder.updateBuildInfo('addSourceFiles','add.c',path);
coder.cinclude('add.h');
temp = 0;
temp = coder.ceval('add',x,y);
w = coder.ceval('add',temp,z);
end
end
add4.m
function out = add4(x,y,z,w)
out = 0;
if coder.target('MATLAB')
out = x+y+z+w;
else
path1 = 'D:/codegen/lib/add';
path2 = 'D:/codegen/lib/add3';
coder.updateBuildInfo('addIncludePaths',path1);
coder.updateBuildInfo('addIncludePaths',path2);
coder.updateBuildInfo('addSourceFiles','add.c',path1);
coder.updateBuildInfo('addSourceFiles','add3.c',path2);
coder.cinclude('add.h');
coder.cinclude('add3.h');
temp = 0;
temp = coder.ceval('add3',x,y,z);
out = coder.ceval('add',temp,w);
end
end
Hope this helps !
  2 Comments
Brian Stevens
Brian Stevens on 2 Nov 2020
Dear Rashed,
Thank you for the response. I was able to take your example and build it.
However, to expand on the example to show my exact issue with the rt_nonfinite file.
If you were to force the nonfinite files to be created by changing add.m to:
function z = add(x,y)
z = x+y;
if(isinf(z))
z = 0;
end
end
This includes the rt_nonfinite and other files for NaN and inf.
To suppor this change, I changed add3.m to:
function w = add3(x,y,z)
w = 0;
if coder.target('MATLAB')
w = x+y+z;
else
path = 'C:\MATLAB\Modular_Code_Gen\codegen\lib\add';
coder.updateBuildInfo('addIncludePaths',path);
coder.updateBuildInfo('addSourceFiles','add.c',path);
coder.updateBuildInfo('addSourceFiles','add_initialize.c',path);
coder.updateBuildInfo('addSourceFiles','add_terminate.c',path);
coder.updateBuildInfo('addSourceFiles','rt_nonfinite.c',path);
coder.updateBuildInfo('addSourceFiles','rtGetInf.c',path);
coder.updateBuildInfo('addSourceFiles','rtGetNaN.c',path);
coder.cinclude('add.h');
coder.cinclude('add_initialize.h');
coder.cinclude('add_terminate.h');
coder.cinclude('add_data.h');
coder.cinclude('add_types.h');
coder.cinclude('rt_nonfinite.h');
coder.cinclude('rtGetInf.h');
coder.cinclude('rtGetNaN.h');
% Executing in the generated code.
% Call the initialize function before calling the
% C function for the first time
coder.ceval('add_initialize');
% Call the generated C library function myabsval
temp = 0;
temp = coder.ceval('add',x,y);
w = coder.ceval('add',temp,z);
% Call the terminate function after
% calling the C function for the last time
coder.ceval('add_terminate');
end
end
Here, I am adding the init and terminate functions and these additional support files {rt_nonfinite,rtGetInf,rtGetNaN}
However, when I go to the "Check for Run-Time Issues" tab of the add3 project I get these errors:
ninja: error: build.ninja:46: multiple rules generate build/win64/rt_nonfinite.obj [-w dupbuild=err]
??? Build error: C compiler produced errors. See the Build Log for further details.
More information
Code generation failed: View Error Report
I understand that both add and add3 are trying to create the same support files. But how can I get add included in add3 with these support files?
What I have tried:
  1. Adding "custom code" under the add3 project for the duplicate files
  2. Selecting the option for the add project "Generate nonfinite support files if used" as "No" (this doesnt seem to change anything in the files produced)
Let me know if you see something similar or have an idea on how to get around this?
Brian
Jean-Pierre Theron
Jean-Pierre Theron on 29 Sep 2022
Hi Brain
Did Mathworks provide you with a solution to this problem? I am experiencing a similar problem with the nonfinite number support files while trying to combine autocoded MATLAB code with an autocoded Simulink model.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!