Documentation

coder.ceval

呼叫外部C / C ++函数

Syntax

coder.ceval(cfun_name)
coder.ceval(cfun_name,cfun_arguments)
coder.ceval('-global',cfun_name)
coder.ceval('-global',cfun_name,cfun_arguments)
coder.ceval('-layout:rowMajor',cfun_name,cfun_arguments)
coder.ceval('-layout:columnMajor',cfun_name,cfun_arguments)
coder.ceval('-layout:any',cfun_name,cfun_arguments)
cfun_return = coder.ceval(___)

描述

example

coder.ceval(cfun_name)executes the external C/C++ function specified bycfun_name。定义cfun_name在外部C / C ++源文件或库中。为代码生成器提供外部源,库和标头文件。

example

coder.ceval(cfun_name,cfun_arguments)executescfun_namewith argumentscfun_argumentscfun_argumentsis a comma-separated list of input arguments in the order thatcfun_name需要。

默认,coder.cevalpasses arguments by value to the C/C++ function whenever C/C++ supports passing arguments by value. To makecoder.cevalpass arguments by reference, use the constructscoder.ref,coder.rref, andcoder.wref。If C/C++ does not support passing arguments by value, for example, if the argument is an array,coder.cevalpasses arguments by reference. If you do not usecoder.ref,coder.rreforcoder.wref, a copy of the argument can appear in the generated code to enforce MATLAB®semantics for arrays.

example

coder.ceval('-global',cfun_name)executescfun_name并表示cfun_nameuses one or more MATLAB global variables. The code generator can then produce code that is consistent with this global variable usage.

coder.ceval('-global',cfun_name,cfun_arguments)executescfun_namewith argumentscfun_arguments并表示cfun_nameuses one or more MATLAB global variables.

example

coder.ceval('-layout:rowmajor',cfun_name,cfun_arguments)executescfun_namewith argumentscfun_argumentsand passes data stored in row-major layout. When called from a function that uses column-major layout, the code generator converts inputs to row-major layout and converts outputs back to column-major layout. For a shorter syntax, usecoder.ceval('-row',...)

coder.ceval('-layout:columnMajor',cfun_name,cfun_arguments)executescfun_namewith argumentscfun_arguments并通过数据存储在列为主的布局。当n called from a function that uses row-major layout, the code generator converts inputs to column-major layout and converts outputs back to row-major layout. For a shorter syntax, usecoder.ceval('-col',...)

coder.ceval('-layout:任何',cfun_name,cfun_arguments)executescfun_namewith argumentscfun_argumentsand passes data with its current array layout, even when array layouts do not match. The code generator does not convert the array layout of the input or output data.

example

cfun_return = coder.ceval(___)executescfun_nameand returns a single scalar value,cfun_return, corresponding to the value that the C/C++ function returns in thereturnstatement. To be consistent with C/C++,coder.cevalcan return only a scalar value. It cannot return an array. Use this option with any of the input argument combinations in the previous syntaxes.

例子

collapse all

Call a C functionfoo(u)来自您打算生成C代码的MATLAB功能。

Create a C header filefoo.hfor a functionfoothat takes two input parameters of type双倍的and returns a value of type双倍的

双倍的foo(double in1, double in2);

Write the C functionfoo.c

#include  #include  #include“foo.h”双重foo(double In1,Double In2){返回In1 + In2;}

Write a functioncallfoo那个呼唤fooby usingcoder.ceval。Provide the source and header files to the code generator in the function.

functiony = callfoo.%#codegeny = 0.0;ifcoder.target('matlab')% Executing in MATLAB, call MATLAB equivalent of% C function fooy = 10 + 20;else%在生成的代码中执行,调用C函数foocoder.updatebuildinfo.('addSourceFiles','foo.c'); coder.cinclude('foo.h'); y = coder.ceval('foo', 10, 20);endend

Generate C library code for functioncallfoo。这Codegen.函数生成C代码\codegen\lib\callfoosubfolder.

Codegen.-config:libcallfoo-报告

从Matlab代码调用C库函数。

Write a MATLAB functionmyabsval

functiony = myabsval(u)%#codegeny = abs(u);

生成C静态库myabsval, using the-args选项指定输入参数的大小,类型和复杂性。

Codegen.-config:libmyabsval-args{0.0}
Codegen.function creates the library filemyabsval.lib和标题文件myabsval.h.in the folder\codegen\lib\myabsval。(The library file extension can change depending on your platform.) It generates the functionsmyabsval_initializeandmyabsval_terminatein the same folder.

编写MATLAB函数来调用生成的C库功能coder.ceval

functiony = callmyabsval(y)%#codegen% Check the target. Do not use coder.ceval if callmyabsval is% executing in MATLABifcoder.target('matlab')%在matlab中执行,调用函数myabsvaly = myabsval(y);else% add the required include statements to generated function codecoder.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 timecoder.ceval('myabsval_initialize');% Call the generated C library function myabsvaly = coder.ceval('myabsval',y);% Call the terminate function after%在最后一次调用C函数coder.ceval('myabsval_terminate');end

Generate the MEX functioncallmyabsval_mex。Provide the generated library file at the command line.

Codegen.-config:mexcallmyabsvalCodegen.\lib\myabsval\myabsval.lib-args{-2.75}

Rather than providing the library at the command line, you can usecoder.updatebuildinfo.to specify the library within the function. Use this option to preconfigure the build. Add this line to theelseblock:

coder.updatebuildinfo.('addLinkObjects','myabsval.lib','$(start_dir)\ codegen \ lib \ myabsval',100,true,true);

Run the MEX functioncallmyabsval_mexwhich calls the library functionmyabsval

callmyabsval_mex(-2.75)
ans = 2.7500

Call the MATLAB functioncallmyabsval

callmyabsval(-2.75)
ans = 2.7500
callmyabsval功能展示了在MATLAB和代码生成中执行的所需行为。

Use the'-global'flag when you call a C function that modifies a global variable.

Write a MATLAB functionumerglobal.调用C函数addGlobal。Use the'-global'flag to indicate to the code generator that the C function uses a global variable.

functiony = useGlobal()globalG;t = g;% compare execution with/without '-global' flagcoder.ceval('-global','addglobal'); y = t;end

Create a C header fileaddGlobal.hfor the functionaddGlobal

void addGlobal(void);

Write the C functionaddGlobal在文件中addGlobal.c。This function includes the header fileumerglobal._data.hthat the code generator creates when you generate code for the functionumerglobal.。This header file contains the global variable declaration forg

#include "addGlobal.h" #include "useGlobal_data.h" void addGlobal(void) { g++; }

生成MEX功能umerglobal.。要将输入定义到代码生成器,请在工作区中声明全局变量。

globalG;g = 1;Codegen.umerglobal.-报告addGlobal.haddGlobal.cy = useGlobal_mex();

With the'-global'标志,MEX功能会产生结果y = 1。这'-global'flag indicates to the code generator that the C function possibly modifies the global variable. Forumerglobal.,代码生成器会产生此代码:

real_T useGlobal(const emlrtStack *sp) { real_T y; (void)sp; y = g; addGlobal(); return y; }

Without the'-global'标志,MEX功能会产生y = 2。因为没有指示C功能修改g, the code generator assumes thatyandgare identical. This C code is generated:

real_t使用global(const emlrtstack * sp){(void)sp;addglobal();返回g;}

假设你有一个c函数testRM这旨在使用行主要布局。您希望将此功能集成到MATLAB函数中barthat operates on arrays. The functionbaris designed to use column-major layout, employing thecoder.columnMajordirective.

function出去= bar(in)%#codegencoder.columnMajor; coder.ceval('-layout:rowmajor','testrm',。。。coder.rref(in),coder.wref(out));end

In the generated code, the code generator inserts a layout conversion from column-major layout to row-major layout on the variableinbefore passing it totestRM。在输出变量上出去,代码生成器将布局转换返回到列柱中。

In general, if you do not specify thelayout选择coder.ceval, the external function arguments are assumed to use column-major.

Input Arguments

collapse all

Name of external C/C++ function to call.

例子:coder.ceval('foo')

Data Types:char|string

Comma-separated list of input arguments in the order thatcfun_name需要。

例子:coder.ceval('foo', 10, 20);

例子:coder.ceval('myFunction', coder.ref(x));

Data Types:single|双倍的|int8|INT16|INT32.|INT64.|uint8|uint16|uint32|uint64|logical|char|struct
Complex Number Support:Yes

Limitations

  • You cannot usecoder.cevalon functions that you declare extrinsic withcoder.extrinsic

  • 当n the LCC compiler creates a library, it adds a leading underscore to the library function names. If the compiler for the library was LCC and your code generation compiler is not LCC, you must add the leading underscore to the function name, for example,coder.ceval('_mylibfun')。If the compiler for a library was not LCC, you cannot use LCC to generate code from MATLAB code that calls functions from that library. Those library function names do not have the leading underscore that the LCC compiler requires.

  • If a property has a get method, a set method, or validators, or is a System object™ property with certain attributes, then you cannot pass the property by reference to an external function. SeePassing By Reference Not Supported for Some Properties

提示

  • 对于代码生成,请在主叫之前coder.ceval,您必须指定返回值和输出参数的类型,大小和复杂性数据类型。

  • Usecoder.cevalonly in MATLAB for code generation.coder.cevalgenerates an error in uncompiled MATLAB code. To determine if a MATLAB function is executing in MATLAB, usecoder.target。如果函数执行在MATLAB调用马铃薯草version of the C/C++ function.

Introduced in R2011a

Was this topic helpful?