首页 > 解决方案 > 使用 boolean_T 的 Matlab 编码器

问题描述

我正在尝试为一个简单的函数 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 但未在任何地方声明。在我看来,没有包含带有声明的标题。生成代码的脚本是:

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 类型?或者有一些我应该使用的指令,但我错过了?

标签: matlabmatlab-coder

解决方案


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


推荐阅读