使用 boolean_T 的 Matlab 编码器

Matlab Coder using boolean_T

我正在尝试为一个简单的函数 Matlab 函数生成 C 代码:

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

但是,在生成的代码中使用了变量类型 boolean_T 但未在任何地方声明。在我看来,没有包含 header 及其声明。 生成代码的脚本是:

config_obj = coder.config('exe');
config_obj.GenCodeOnly = 'on';
codegen -config config_obj myfun

通过使用自定义 makefile 调用 make,我收到以下错误消息:

error: unknown type name 'boolean_T'
error: 'false' undeclared (first use in this function)
error: 'true' undeclared (first use in this function)

我可以请求单个文件并添加自定义代码:

config_obj = coder.FilePArtitioningMethod('SingleFile');
config_obj.CustomSourceCode = ['typedef unsigned int boolean_T;',newline,...
                               '#define true 1U',newline,...
                               '#define false 0U'];

这将使我能够正确地编译代码,但这是一个蹩脚的解决方案,因为我不想生成单个文件,并且添加的源代码并没有根据需要包含在每个文件中。

有什么方法可以避免使用 boolean_T 类型?或者有一些我应该使用但我丢失的指令?

boolean_T 和 int_T 等可能的其他类型在 header 文件中定义,这些文件不是生成的,而是随 MATLAB 一起提供的。通常定义在 tmwtypes.h 中,您可以在 /extern/include 中找到。生成的 makefile 在包含目录列表中包括指向此的路径作为编译器的选项。如果您不使用生成的 makefile,则需要将这些 header 的路径手动添加到您的编译器选项中。