Main Content

calllib

Call function in C shared library

Description

example

[x1,...,xN] = calllib(libname,funcname,arg1,...,argN)calls functionfuncnamein C librarylibname, passing input argumentsarg1,...,argN. Thecalllibfunction returns output values obtained fromfuncnameinx1,...,xN.

Examples

collapse all

Load the library.

if~libisloaded('shrlibsample') addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample')end

Display function signature.

libfunctionsviewshrlibsample
[double, c_structPtr] addStructByRef(c_structPtr)

The input argument is a pointer to ac_structdata type.

Create a MATLAB®structure,struct:

struct.p1 = 4; struct.p2 = 7.3; struct.p3 = -290;

Call the function.

[res,st] = calllib('shrlibsample','addStructByRef',struct);

显示结果。

res
res = -279

Cleanup.

unloadlibraryshrlibsample

Input Arguments

collapse all

Name of shared library, specified as a character vector. Do not include the path or file extension inlibname.

If you callloadlibraryusing thealiasoption, then you must use the alias name for thelibnameargument.

Data Types:char

Name of function in library, specified as a character vector.

Data Types:char

Input arguments, 1 through N, required byfuncname(if any), specified by any type. Thefuncnameargument list specifies the argument type.

Output Arguments

collapse all

Output arguments, 1 through N, fromfuncname(if any), returned as any type. Thefuncnameargument list specifies the argument type.

Limitations

  • Use with libraries that are loaded using theloadlibraryfunction.

Tips

  • MATLAB validates input argument types before callingfuncname. If MATLAB displays error messages about data types, check the MATLAB function signature. For example, iffuncnameis in librarymylibtype:

    libfunctions('mylib','-full')

    To findfuncname, scroll through the output. For more information, refer to your library documentation.

    When you callfuncname, that function might display errors. For information about error messages, refer to your library documentation.

Introduced before R2006a