Main Content

生成多个入口点功能的代码

An entry-point function is a top-level MATLAB®您生成代码的函数。对于许多美联社plications, you may only need to generate code for a single entry-point function. You can also generate C/C++ code from multiple entry-point functions at the same time. By using multiple entry-point functions, you can:

  • 产生multi-functional C/C++ libraries that contain larger levels of functionality than if you were to generate independent libraries for each entry-point function.

  • 当多个入口点功能依赖相同的子功能时,生成代码更有效地共享代码。

  • 产生library functions that can communicate using shared memory, for example, when they use the same global variables.

As a best practice, generate a MEX function to validate entry-point interactions in MATLAB before generating a C/C++ library.

生成多个入口点功能的代码

要为多个入口点函数生成代码,请使用来自代码根参考页。默认情况下,对于MEX代码生成,代码根

  • 在当前文件夹中生成MEX函数。指定多个入口点函数时,仅生成一个MEX函数。要从生成的MEX功能调用单个入口点函数,请参见从MEX函数调用单个入口点功能

  • 命名MEX功能姓名_mex姓名是第一个入口点函数的名称nalphabeticalorder.

  • Stores generated files in the subfolder代码根/mex/subfoldersubfolder是第一个入口点函数的名称左到右订单(当它们被输入之后代码根command).

您可以使用该名称指定输出文件名和子文件夹名称-ooption:

代码根-omyOutputFileNamefun1fun2

在这种情况下,代码根生成一个名称的MEX功能myOutputFileNamein the current folder and stores generated files in the subfolder代码根/mex/myOutputFileName

示例:为两个入口点函数生成代码

产生a MEX function for two entry-point functions,EP1andEP2。功能EP1takes one input andEP2takes two inputs. Using the-ooption, name the generated MEX function共享mex

代码根-omySharedMexEP1-args{single(0)}EP2-args{0,零(1,1024)}

代码根生成一个名称的MEX功能mySharedMex.mexin the current folder and stores generated files in the subfolder代码根/mex/mySharedMex

要生成和编译独立库代码,请使用-config:liboption.

代码根-config:lib-omysharedlibEP1-args单(0)EP2-args{0,零(1,1024)}

代码根command generates the C/C++ library code in theCodegen/lib/mysharedlib文件夹。

To use the output type from one entry-point function as the input type to another, seePass an Entry-Point Function Output as an Input。For information on viewing entry-point functions in the code generation report, see代码生成报告

从MEX函数调用单个入口点功能

Suppose that you have a MEX functionmymexgenerated from multiple entry-point functions,fun1,,,,fun2,,,,…,乐趣。您可以调用单个入口点功能,fun_i,,,,by using this syntax:

mymex('fun_i',,,,param1,..,paramM)

这里的MATLAB功能签名fun_iisfun_i(param1,..,paramM)

For example, consider the MEX function,mySharedMex,具有入口点功能EP1andEP2。打电话EP1with an input parameter, 进入:

mySharedMex('ep1',,,,你)

打电话EP2带有输入参数vandX, 进入:

mySharedMex('ep2',,,,v,,,,X)

使用以上的入口点函数生成代码MATLABCoder应用程序

此示例显示了如何使用该示例为多个入口点函数生成代码MATLAB CODER™应用程序。

创建入口点功能

  1. 在本地写入文件夹中,创建一个matlab文件,EP1.M,,,,that contains:

    functiony = ep1(u)%#codegeny = u;

  2. In the same local writable folder, create a MATLAB file,ep2.m,,,,that contains:

    functiony = ep2(u, v)%#codegeny = u + v;

创建测试文件

在包含的文件夹中EP1.Mandep2.m,,,,create a MATLAB file,ep_test.m,,,,that callsEP1andEP2with example inputs.

function[y,y1] = ep_test y = ep1(单(2));y1 = ep2(double(3),double(4));

打开MATLABCoder应用程序

在MATLAB工具条上应用标签,下代码生成,,,,click theMATLAB编码器app icon.

Specify Source Files

  1. 选择源文件page, type or select the name of the entry-point functionEP1

    该应用程序创建一个带有默认名称的项目EP1。prjin the current folder. To avoid code generation errors, you must store the project file and all entry-point MATLAB function files in the same folder.

  2. 添加EP2到入口点功能列表,请单击Add Entry-Point Function。键入或选择入口点功能的名称EP2

  3. To go to theDefine Input Typesstep, clickNext。该应用程序分析编码问题和代码生成准备就绪的功能。如果该应用确定问题,它将打开Review Code Generation Readinesspage where you can review and fix issues. In this example, because the app does not detect issues, it opens theDefine Input Typespage.

Define Input Types

Because C uses static typing, at compile time,MATLAB编码器must determine the properties of all variables in the MATLAB files. You must specify the properties of all entry-point function inputs. From the properties of the entry-point function inputs,MATLAB编码器can infer the properties of all variables in the MATLAB files.

Specify a test file thatMATLAB编码器can use to automatically define types:

  1. 输入或选择测试文件ep_test.m

  2. 点击Autodefine Input Types

    测试文件,ep_test.m,调用入口点功能EP1andEP2with the example input types.MATLAB编码器进一步EP1,,,,inputis单个(1x1)。ForEP2,,,,andvdouble(1x1)

  3. To go to the检查运行时间问题step, clickNext

检查运行时间问题

检查运行时间问题步骤从您的入口点功能中生成MEX文件,运行MEX函数并报告问题。此步骤是可选的。但是,执行此步骤是最好的做法。您可以检测和修复在生成的C代码中难以诊断的运行时错误。

  1. 打开检查运行时间问题dialog box, click the检查问题arrow

    该应用程序填充了测试文件字段ep_test,您用来定义输入类型的测试文件。

  2. 点击检查问题

    这app generates a MEX function namedEP1_mex为了EP1andEP2。它运行测试文件ep_testreplacing calls toEP1andEP2带有对MEX功能的呼叫。如果该应用在MEX功能生成或执行过程中检测到问题,则提供警告和错误消息。要导航到有问题的代码并解决问题,请单击这些消息。在此示例中,该应用程序未检测到问题。

  3. To go to the产生Codestep, clickNext

产生MEX Function

  1. 打开产生dialog box, click the产生arrow

  2. 构建类型MEX

  3. 验证输出文件名isEP1_mex。By default, the app uses the name of the alphabetically first entry-point function.

  4. 点击产生

    MATLAB编码器builds the project. It generates a MEX function,EP1_mex,,,,in the current folder.MATLAB编码器also generates other supporting files in a subfolder calledcodegen/mex/ep1_mexMATLAB编码器你ses the name of the MATLAB function as the root name for the generated files. It creates a platform-specific extension for the MEX file, as described in命名约定

    您现在可以在MATLAB中测试MEX功能。看从MEX函数调用单个入口点功能

    如果您生成一个静态库EP1andEP2,,,,MATLAB编码器构建项目并生成C库,EP1,并支持默认万博1manbetx文件夹中的文件,代码根/lib/ep1

也可以看看

|

Related Topics