从 MATLAB 代码生成独立的 C/C++ 可执行文件 您所在的位置:网站首页 怎么用gcc运行c程序 从 MATLAB 代码生成独立的 C/C++ 可执行文件

从 MATLAB 代码生成独立的 C/C++ 可执行文件

2023-05-18 05:11| 来源: 网络整理| 查看: 265

创建入口函数

在本地可写文件夹中,创建 MATLAB 函数 coderand,该函数在开区间 (0,1) 上基于标准均匀分布生成一个随机标量值:

function r = coderand() %#codegen r = rand();

创建测试文件

在同一本地可写文件夹中,创建 MATLAB 文件 coderand_test.m,它调用 coderand。

function y = coderand_test() y = coderand();

打开 MATLAB Coder App

在 MATLAB 工具条的 App 选项卡上,点击代码生成下的 MATLAB Coder App 图标。

该 App 会打开选择源文件页面。

指定源文件

在选择源文件页面中,键入或选择入口函数 coderand 的名称。

该 App 将使用默认名称 coderand.prj 在当前文件夹中创建一个工程。

点击下一步以转到定义输入类型步骤。该 App 将分析函数以查找编码问题并确定代码生成就绪情况。如果 App 发现问题,它将打开检查代码就绪性页面,您可以在其中查看和解决问题。在此示例中,由于 App 没有检测到问题,因此将打开定义输入类型页面。

定义输入类型

由于 C 使用静态定型,MATLAB Coder 必须在编译时确定 MATLAB 文件中所有变量的属性。您必须指定所有入口函数输入的属性。根据入口函数输入的属性,MATLAB Coder 可以推断 MATLAB 文件中所有变量的属性。

在此示例中,函数 coderand 没有输入。

点击下一步以转到检查运行时问题步骤。

检查运行时问题

检查运行时问题步骤从您的入口函数生成 MEX 文件,然后运行 MEX 函数并报告问题。此步骤是可选的。不过,建议最好执行此步骤。您可以检测并解决在生成的 C 代码中更难诊断出来的运行时错误。

要打开检查运行时问题对话框,请点击检查问题箭头 。

选择或输入测试文件 coderand_test。

点击检查问题。

App 将为 coderand 生成一个 MEX 函数。它运行测试文件,将对 coderand 的调用替换为对 MEX 函数的调用。如果 App 在 MEX 函数生成或执行过程中检测到问题,它将提供警告和错误消息。您可以点击这些消息,导航到有问题的代码并修复问题。在本示例中,App 未检测到问题。

点击下一步以转到生成代码步骤。

生成 C main 函数

在生成可执行文件时,您必须提供一个 C/C++ 主函数。默认情况下,当您生成 C/C++ 源代码、静态库、动态链接库或可执行文件时,MATLAB Coder 会生成 main 函数。这一生成的主函数是一个模板,您可以针对您的应用程序修改该模板。请参阅使用示例主函数合并生成的代码。在复制和修改生成的主函数后,可以使用它来生成 C/C++ 可执行文件。您也可以编写自己的主函数。

在为 coderand 生成可执行文件之前,请生成 main 函数,该函数调用 coderand。

要打开生成对话框,请点击生成 箭头 。

在生成对话框中,将编译类型设置为“源代码”,将语言设置为 C。对于其他工程编译配置设置,请使用默认值。

点击更多设置。

在所有设置选项卡的高级下,确认生成示例主函数设置为“生成但不编译示例主函数”。点击关闭。

点击生成。

MATLAB Coder 生成一个 main.c 文件和一个 main.h 文件。App 指示代码生成成功。

点击下一步打开完成工作流页面。

在完成工作流页面的生成的输出下,您会看到 main.c 位于子文件夹 coderand\codegen\lib\coderand\examples 中。

复制生成的示例主文件

由于后续代码生成可能覆盖生成的示例文件,因此在修改这些文件之前,请将它们复制到 codegen 文件夹之外的一个可写文件夹中。对于此示例,请将 main.c 和 main.h 从子文件夹 coderand\codegen\lib\coderand\examples 复制到一个可写文件夹中,例如 c:\myfiles。

修改生成的示例主文件

在包含示例主文件副本的文件夹中,打开 main.c。

 生成的 main.c

/*************************************************************************/ /* This automatically generated example C main file shows how to call */ /* entry-point functions that MATLAB Coder generated. You must customize */ /* this file for your application. Do not modify this file directly. */ /* Instead, make a copy of this file, modify it, and integrate it into */ /* your development environment. */ /* */ /* This file initializes entry-point function arguments to a default */ /* size and value before calling the entry-point functions. It does */ /* not store or use any values returned from the entry-point functions. */ /* If necessary, it does pre-allocate memory for returned values. */ /* You can use this file as a starting point for a main function that */ /* you can deploy in your application. */ /* */ /* After you copy the file, and before you deploy it, you must make the */ /* following changes: */ /* * For variable-size function arguments, change the example sizes to */ /* the sizes that your application requires. */ /* * Change the example values of function arguments to the values that */ /* your application requires. */ /* * If the entry-point functions return values, store these values or */ /* otherwise use them as required by your application. */ /* */ /*************************************************************************/ /* Include Files */ #include "main.h" #include "coderand.h" #include "coderand_terminate.h" /* Function Declarations */ static void main_coderand(void); /* Function Definitions */ /* * Arguments : void * Return Type : void */ static void main_coderand(void) { double r; /* Call the entry-point 'coderand'. */ r = coderand(); } /* * Arguments : int argc * const char * const argv[] * Return Type : int */ int main(int argc, const char * const argv[]) { (void)argc; (void)argv; /* The initialize function is being called automatically from your entry-point function. So, a call to initialize is not included here. */ /* Invoke the entry-point functions. You can call entry-point functions multiple times. */ main_coderand(); /* Terminate the application. You do not need to do this more than one time. */ coderand_terminate(); return 0; } /* * File trailer for main.c * * [EOF] */

修改 main.c,使其打印 coderand 调用的结果:

在 main_coderand 中,删除以下行

double r;

在 main_coderand 中,将

r = coderand() 替换为: printf("coderand=%g\n", coderand());

对于此示例,main 没有参数。在 main 中,删除以下行:

(void)argc; (void)argv;

将 main 的定义更改为

int main()

 修改后的 main.c

/* Include Files */ #include "main.h" #include "coderand.h" #include "coderand_terminate.h" /* Function Declarations */ static void main_coderand(void); /* Function Definitions */ /* * Arguments : void * Return Type : void */ static void main_coderand(void) { /* Call the entry-point 'coderand'. */ printf("coderand=%g\n", coderand()); } /* * Arguments : int argc * const char * const argv[] * Return Type : int */ int main() { /* The initialize function is being called automatically from your entry-point function. So, a call to initialize is not included here. */ /* Invoke the entry-point functions. You can call entry-point functions multiple times. */ main_coderand(); /* Terminate the application. You do not need to do this more than one time. */ coderand_terminate(); return 0; } /* * File trailer for main.c * * [EOF] */

打开 main.h。

 生成的 main.h

/*************************************************************************/ /* This automatically generated example C main file shows how to call */ /* entry-point functions that MATLAB Coder generated. You must customize */ /* this file for your application. Do not modify this file directly. */ /* Instead, make a copy of this file, modify it, and integrate it into */ /* your development environment. */ /* */ /* This file initializes entry-point function arguments to a default */ /* size and value before calling the entry-point functions. It does */ /* not store or use any values returned from the entry-point functions. */ /* If necessary, it does pre-allocate memory for returned values. */ /* You can use this file as a starting point for a main function that */ /* you can deploy in your application. */ /* */ /* After you copy the file, and before you deploy it, you must make the */ /* following changes: */ /* * For variable-size function arguments, change the example sizes to */ /* the sizes that your application requires. */ /* * Change the example values of function arguments to the values that */ /* your application requires. */ /* * If the entry-point functions return values, store these values or */ /* otherwise use them as required by your application. */ /* */ /*************************************************************************/ #ifndef MAIN_H #define MAIN_H /* Include Files */ #include #include #include "rtwtypes.h" #include "coderand_types.h" /* Function Declarations */ extern int main(int argc, const char * const argv[]); #endif /* * File trailer for main.h * * [EOF] */

修改 main.h:

将 stdio 添加到包含文件中:

#include

将 main 的声明更改为

extern int main()

 修改后的 main.h

#ifndef MAIN_H #define MAIN_H /* Include Files */ #include #include #include #include "rtwtypes.h" #include "coderand_types.h" /* Function Declarations */ extern int main(); #endif /* * File trailer for main.h * * [EOF] */

生成可执行文件

在修改生成的示例主文件后,打开之前创建的工程文件或从 App 中选择 coderand.m。您可以选择覆盖该工程,或以不同名称命名工程以同时保存这两个工程文件。

要打开生成代码页面,请展开工作流步骤 ,然后点击生成

要打开生成对话框,请点击生成 箭头 。

将编译类型设置为“可执行文件(.exe)”。

点击更多设置。

在自定义代码选项卡的其他源文件中,输入 main.c

在自定义代码选项卡的其他包括目录中,输入修改后的 main.c 和 main.h 文件的位置。例如,c:\myfiles。点击关闭。

要生成可执行文件,请点击生成。

App 指示代码生成成功。

点击下一步以转至完成工作流步骤。

在生成的输出下,您可以看到生成的可执行文件 coderand.exe 的位置。

运行可执行文件

要在 Windows® 平台上的 MATLAB 中运行可执行文件,请执行以下命令:

system('coderand')

注意

此示例中可执行的 coderand 函数返回介于 0 到 1 之间的固定值。要生成每次执行都会产生新值的代码,请在 MATLAB 代码中使用 RandStream 函数。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有