coder.target
确定代码生成目标是否为指定的目标
说明
示例
参数化 MATLAB 函数,使其能够在 MATLAB 或生成代码中工作。当函数在 MATLAB 中运行时,它会调用 MATLAB 函数 myabsval。然而,生成的代码会调用 C 库函数 myabsval。
编写 MATLAB 函数 myabsval。
function y = myabsval(u) %#codegen y = abs(u);
通过使用 -args 选项指定输入参数的大小、类型和复/实性,为 myabsval 生成 C 静态库。
codegen -config:lib myabsval -args {0.0}
codegen 函数在文件夹 \codegen\lib\myabsval 中创建库文件 myabsval.lib 和头文件 myabsval.h。(库文件扩展名可以根据您的平台而有所不同。)它在同一个文件夹中生成函数 myabsval_initialize 和 myabsval_terminate。编写一个 MATLAB 函数,以使用 coder.ceval 调用生成的 C 库函数。
function y = callmyabsval(y) %#codegen % Check the target. Do not use coder.ceval if callmyabsval is % executing in MATLAB if coder.target('MATLAB') % Executing in MATLAB, call function myabsval y = myabsval(y); else % add the required include statements to generated function code coder.updateBuildInfo('addIncludePaths','$(START_DIR)\codegen\lib\myabsval'); coder.cinclude('myabsval_initialize.h'); coder.cinclude('myabsval.h'); coder.cinclude('myabsval_terminate.h'); % Executing in the generated code. % Call the initialize function before calling the % C function for the first time coder.ceval('myabsval_initialize'); % Call the generated C library function myabsval y = coder.ceval('myabsval',y); % Call the terminate function after % calling the C function for the last time coder.ceval('myabsval_terminate'); end
生成 MEX 函数 callmyabsval_mex。在命令行中提供生成的库文件。
codegen -config:mex callmyabsval codegen\lib\myabsval\myabsval.lib -args {-2.75}
您可以使用 coder.updateBuildInfo 在函数中指定该库,而不是在命令行中提供库。使用此选项可预配置编译。将以下行添加到 else 模块中:
coder.updateBuildInfo('addLinkObjects','myabsval.lib','$(START_DIR)\codegen\lib\myabsval',100,true,true);
运行调用库函数 myabsval 的 MEX 函数 callmyabsval_mex。
callmyabsval_mex(-2.75)
ans =
2.7500调用 MATLAB 函数 callmyabsval。
callmyabsval(-2.75)
ans =
2.7500callmyabsval 函数在 MATLAB 和代码生成中的执行均展示出了预期的行为。输入参数
代码生成目标,指定为字符向量或字符串标量。指定下列目标之一。
'MATLAB' | 在 MATLAB 中运行(不生成代码) |
| 代码生成支持的目标语言 |
'MEX' | 生成 MEX 函数 |
'Sfun' | 仿真 Simulink® 模型。也用于在加速模式下运行。 |
'Rtw' | 生成 LIB、DLL 或 EXE 目标。也用于在 Simulink Coder™ 和快速加速模式下运行。 |
'HDL' | 生成 HDL 目标 |
'Custom' | 生成自定义目标 |
示例: tf = coder.target('MATLAB')
示例: tf = coder.target("MATLAB")
注意
对于 CUDA 或 High-Level Synthesis (HLS) 代码生成, coder.target('C++') 始终为 true。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。
版本历史记录
在 R2011a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)