Create C Shared Library with MATLAB Code
Supported platform: Windows®, Linux®, Mac
This example shows how to create a C shared library using a MATLAB® function. The target system does not require a licensed copy of MATLAB.
Create Functions in MATLAB
In MATLAB, examine the MATLAB code that you want packaged.
For this example, copy the
matrix
folder that ships with MATLAB to your work folder.copyfile(fullfile(matlabroot,'extern','examples','compilersdk','c_cpp','matrix'),'matrix')
Navigate to the new
matrix
subfolder in your work folder.Examine and test the functions
addmatrix.m
,multiplymatrix.m
, andeigmatrix.m
.At the MATLAB command prompt, enter
addmatrix([1 4 7; 2 5 8; 3 6 9], [1 4 7; 2 5 8; 3 6 9])
.The output is:
ans = 2 8 14 4 10 16 6 12 18
At the MATLAB command prompt, enter
multiplymatrix([1 4 7; 2 5 8; 3 6 9], [1 4 7; 2 5 8; 3 6 9])
.The output is:
ans = 30 66 102 36 81 126 42 96 150
At the MATLAB command prompt, enter
eigmatrix([1 4 7; 2 5 8; 3 6 9])
.The output is:
ans = 16.1168 -1.1168 -0.0000
Create C Shared Library Using compiler.build.cSharedLibrary
Build the C shared library using the
compiler.build.cSharedLibrary
function. Use name-value arguments to specify the library name and enable verbose output.buildResults = compiler.build.cSharedLibrary(["addmatrix.m", ... "eigmatrix.m","multiplymatrix.m"], ... 'LibraryName','libmatrix', ... 'Verbose','on');
You can specify additional options in the
compiler.build
command by using name-value arguments. For details, seecompiler.build.cSharedLibrary
.The
compiler.build.Results
objectbuildResults
contains information on the build type, generated files, included support packages, and build options.The function generates the following files within a folder named
libmatrixcSharedLibrary
in your current working directory:GettingStarted.html
— HTML file that contains information on integrating your shared library.includedSupportPackages.txt
— Text file that lists all support files included in the library.libmatrix.c
— C source code file.libmatrix.def
— Module-definition file that provides the linker with module information.libmatrix.dll
— Dynamic-link library file.libmatrix.exports
— Exports file that contains all nonstatic function names.libmatrix.h
— C header file.libmatrix.lib
— Import library file. The file extension is.dylib
on Mac and.so
on Linux.mccExcludedFiles.log
— Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see MATLAB Compiler Limitations.readme.txt
— Text file that contains packaging information.requiredMCRProducts.txt
— Text file that contains product IDs of products required by MATLAB Runtime to run the application.unresolvedSymbols.txt
— Text file that contains information on unresolved symbols.
Note
The generated library does not include MATLAB Runtime or an installer. To create an installer using the
buildResults
object, seecompiler.package.installer
.
Implement C Shared Library in C Application
After packaging your C shared library, you can call it from a C application. The C application code calls the functions included in the shared library.
Locate the
matrix.c
file located inmatlabroot\extern\examples\compilersdk\c_cpp\matrix
or your work folder.Copy and paste this file into the folder that contains your C library file (
libmatrix.lib
on Windows,libmatrix.dylib
on Mac,libmatrix.so
on Linux).At the MATLAB command prompt, navigate to the folder where you copied
matrix.c
.Use
mbuild
to compile and link the application.mbuild matrix.c libmatrix.<ext>
To run the application on Linux or Mac systems, you must first add MATLAB Runtime to the library path. For details, see Set MATLAB Runtime Path for Deployment.
From the system command prompt, run the application. On Windows, replace the forward slash with a backslash (
\
)../matrix
The sum of the matrix with itself is: 2.00 8.00 14.00 4.00 10.00 16.00 6.00 12.00 18.00 The product of the matrix with itself is: 30.00 66.00 102.00 36.00 81.00 126.00 42.00 96.00 150.00 The eigenvalues of the original matrix are: 16.12 -1.12 -0.00
Note
You may need to give the application executable permissions on Linux or Mac systems.
chmod u+x matrix
See Also
compiler.build.cSharedLibrary
| mxArray (C)