Documentation

Call a Shared Library

To use aMATLAB®Compiler SDK™在您的应用程序中生成共享库:

  1. Include the generated header file for each library in your application.

    Each generated shared library has an associated header file namedlibname。h

  2. Initialize theMATLAB Runtimeproxy layer by callingmclmcrInitialize()

  3. Usemclrunmain()to call the C function where your MATLAB functions are used.

    mclrunmain()provides a convenient cross platform mechanism for wrapping the execution of MATLAB code.

    Caution

    不使用mclrunmain()if your application brings up its own full graphical environment.

  4. Initialize theMATLAB Runtime并通过调用mclInitializeApplication()API功能。

    Call themclInitializeApplication()函数一次每个应用程序,必须在调用任何其他MATLAB API函数之前调用。您可以将应用程序级选项传递给此功能。mclInitializeApplication()返回布尔状态代码。

  5. For eachMATLAB Compiler SDKgenerated shared library that you include in your application, call the initialization function for the library.

    The initialization function performs library-local initialization. It unpacks the deployable archive and starts aMATLAB Runtime实例使用必要的信息来执行该存档中的代码。库初始化功能命名libnameInitialize()。This function returns a Boolean status code.

    笔记

    On Windows®, if you want to have your shared library call a MATLAB shared library, the MATLAB library initialization function (e.g.,Initialize, Terminate, mclInitialize, mclTerminate) cannot be called from your shared library during theDllMain(DLL_ATTACH_PROCESS)称呼。这适用于中间共享库是隐式或明确加载的。在某个地方放置电话DllMain()

  6. Call the exported functions of each library as needed.

  7. When your application no longer needs a given library, call the termination function for the library.

    终止功能可以释放与库相关的资源MATLAB Runtimeinstance. The library termination function is namedlibname终止()。库终止后,该库导出的功能将在应用程序中再次调用。

  8. When your application no longer needs to call anyMATLAB Compiler SDKgenerated libraries, call themclterinateapplicationAPI功能。

    This function frees application-level resources used by theMATLAB Runtime。一旦调用此功能,就无法接听MATLAB Compiler SDKgenerated libraries in the application.

The following code example is frommatrixdriver.c

#include stdio.h / *包括Matlab编译器SDK生成的Matlab Runtime标头文件和特定于库的标头文件 * /#include“ libmatrix.h” / *此函数用于显示存储在mxarray中的双基质*/ void display(const mxarray* in);int run_main(int argc,char ** argv){mxarray *in1, *in2;/ *定义输入参数 */ mxarray *out = null;/ *和要传递到库函数的输出参数 */ double Data [] = {1,2,3,4,5,6,6,7,8,9};/ *创建输入数据 */ in1 = mxcreateDeublemaTrix(3,3,mxreal);in2 = mxCreateDeDoublematrix(3,3,mxreal);memcpy(mxgetpr(in1),数据,9*sizeof(double));memcpy(mxgetpr(in2),数据,9*sizeof(double);/ *调用库初始化例程,并确保 *库是正确初始化的。*/ if(!libmatrixinitialize()){fprintf(stderr,“无法初始化库。\ n”); return -2; } else { /* Call the library function */ mlfAddmatrix(1, &out, in1, in2); /* Display the return value of the library function */ printf("The value of added matrix is:\n"); display(out); /* Destroy the return value since this variable will be reused in * the next function call. Since we are going to reuse the variable, * we have to set it to NULL. Refer to MATLAB Compiler SDK documentation * for more information on this. */ mxDestroyArray(out); out=0; mlfMultiplymatrix(1, &out, in1, in2); printf("The value of the multiplied matrix is:\n"); display(out); mxDestroyArray(out); out=0; mlfEigmatrix(1, &out, in1); printf("The eigenvalues of the first matrix are:\n"); display(out); mxDestroyArray(out); out=0; /* Call the library termination routine */ libmatrixTerminate(); /* Free the memory created */ mxDestroyArray(in1); in1=0; mxDestroyArray(in2); in2 = 0; } /* Note that you should call mclTerminate application at the end of * your application. */ mclTerminateApplication(); return 0; } /*DISPLAY This function will display the double matrix stored in an mxArray. * This function assumes that the mxArray passed as input contains double * array. */ void display(const mxArray* in) { int i=0, j=0; /* loop index variables */ int r=0, c=0; /* variables to store the row and column length of the matrix */ double *data; /* variable to point to the double data stored within the mxArray */ /* Get the size of the matrix */ r = mxGetM(in); c = mxGetN(in); /* Get a pointer to the double data in mxArray */ data = mxGetPr(in); /* Loop through the data and display the same in matrix format */ for( i = 0; i < c; i++ ){ for( j = 0; j < r; j++){ printf("%4.2f\t",data[j*c+i]); } printf("\n"); } printf("\n"); } int main() { /* Call the mclInitializeApplication routine. Make sure that the application * was initialized properly by checking the return status. This initialization * has to be done before calling any MATLAB API's or MATLAB Compiler SDK generated * shared library functions. */ if( !mclInitializeApplication(NULL,0) ) { fprintf(stderr, "Could not initialize the application.\n"); return -1; } return mclRunMain((mclMainFcnType)run_main,0,NULL); }

Restrictions When UsingMATLAB功能负载

您不能使用MATLAB功能loadlibraryinside of MATLAB to load a C shared library built withMATLAB Compiler SDK

For more information about usingloadlibrary, 看在部署的应用程序中调用共享库(MATLAB Compiler).

Was this topic helpful?