create a 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 a c shared library using the library compiler app
build a c shared library using the library compiler app. alternatively, if you want to create a c shared library from the matlab command window using a programmatic approach, see create c shared library using compiler.build.csharedlibrary.
on the matlab apps tab, on the far right of the apps section, click the arrow. in application deployment, click library compiler. in the matlab compiler project window, click c shared library.
alternately, you can open the library compiler app by entering
librarycompiler
at the matlab prompt.in the library compiler app project window, specify the files of the matlab application that you want to deploy.
in the exported functions section of the toolstrip, click .
in the add files window, browse to the example folder, and select the function you want to package. click open.
the function is added to the list of exported function files. repeat this step to package multiple files in the same application.
add
addmatrix.m
,multiplymatrix.m
, andeigmatrix.m
to the list of main files.in the packaging options section of the toolstrip, decide whether to include the matlab runtime installer in the generated application by selecting one of the options:
runtime downloaded from web — generate an installer that downloads the matlab runtime and installs it along with the deployed matlab application. you can specify the file name of the installer.
runtime included in package — generate an application that includes the matlab runtime installer. you can specify the file name of the installer.
note
the first time you select this option, you are prompted to download the matlab runtime installer.
in the library name field, rename the packaged shared library as
libmatrix
. the same name is followed through in the implementation of the shared library.
customize the application and its appearance
in the library compiler app, you can customize the installer, customize your application, and add more information about the application.
library information — information about the deployed application. you can also customize the appearance of the application by changing the application icon and splash screen. the generated installer uses this information to populate the installed application metadata. see .
additional installer options — default installation path for the generated installer and custom logo selection. see .
files required for your library to run — additional files required by the generated application to run. these files are included in the generated application installer. see .
files installed for your end user — files that are installed with your application.
see .
package the application
when you are finished selecting your packaging options, save your library compiler project and generate the packaged application.
click package.
in the save project dialog box, specify the location to save the project.
in the package dialog box, verify that open output folder when process completes is selected.
when the packaging process is complete, examine the generated output in the target folder.
three folders are generated:
for_redistribution
,for_redistribution_files_only
, andfor_testing
.for more information about the files generated in these folders, see .
the log file
packaginglog.html
contains packaging results.
create c shared library using compiler.build.csharedlibrary
as an alternative to the library compiler app, you can create a c shared library using a programmatic approach. if you have already created a library using the library compiler, see implement c shared library in c application.
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, see .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 unix®.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 .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, see .
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 shared library
libmatrix.lib
. if you used the library compiler, it is located in thefor_testing
folder.note
the
.lib
extension is for windows. on mac, the file extension is.dylib
, and on unix it is.so
.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.lib
from the system command prompt, run the application.
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 unix systems by running
chmod u x matrix
see also
| | |