MATLAB: Codegen with custom makefile

boolean_tcompilingmatlab coder

I'm trying to generate C code for a simple function:
function[] = myfunc()
%#codegen
fprintf('Executing myfun\n');
fid = fopen('file_created_by_myfun.txt','w');
fwrite(fid,'This is written by myfun upon execution');
fclose(fid);
end
However, in the generated code a variable type boolean_T is used but not declared anywhere. It seems to me that no header with its declaration was included.
The script to generate the code is:
config_obj = coder.config('exe');
config_obj.GenCodeOnly = 'on';
codegen -config config_obj myfun
I can ask for single file and add custom code with:
config_obj = coder.FilePArtitioningMethod('SingleFile');
config_obj.CustomSourceCode = ['typedef unsigned int boolean_T;',newline,'#define true 1U',newline,'#define false 0U'];
This will allow me to compile the code properly, but it's a crappy solution, since I don't want to generate a single file, and the added source is not included in every file as needed.
Is there any way I can avoid having the boolean_T type being used? Or there some directive I should have used but I'm missing?

Best Answer

Definition of boolean_T is contained inside tmwtypes.h. that file is located in matlabroot/extern/include, where "matlabroot" is the folder where MATLAB is installed. That file contains many other typedefs, it just happened that in your case, only boolean_T is required, but if your source MATLAB code was bigger / more complicated, other defines from tmwtypes.h would be used.
Simply put, the generated C code is not self-sufficient, it needs some .h files that are shipped with MATLAB. To get all the needed .h files and everything else into one place, you can use PackNGo feature, either on the command line:
or as the last step in MATLAB Coder ui (run "coder" command in MATLAB console and follow the prompts).