This example shows how to create a C/C++ shared library using a MATLAB® function. You then integrate it into an application.
In MATLAB, examine the MATLAB code that you want to deploy as a shared library.
Open addmatrix.m
.
function a = addmatrix(a1, a2)
a = a1 + a2;
At the MATLAB command prompt, enter addmatrix(1,2)
.
The output appears as follows:
ans = 3
Open the Library Compiler app.
On the toolstrip, select the Apps tab.
Click the arrow at the far right of the tab.
Click Library Compilers.
In the Application Type section of the toolstrip, select C++ Shared Library from the list.
Specify the MATLAB functions you want to deploy.
In the Exported Functions section of the toolstrip, click the plus button.
In the file explorer that opens, locate and select the addmatrix.m
file.
addmatrix.m
is located in
.matlabroot
\extern\examples\compilersdk
Click Open to select the file and close the file explorer.
In the Packaging Options section of the toolstrip, verify that the Runtime downloaded from web check box is selected.
This option creates an application installer that automatically downloads the MATLAB Runtime and installs it along with the deployed shared library.
Click Package.
The Package window opens while the library is being generated.
Select the Open output folder when process completes check box.
Click Close on the Package window.
Verify the contents of the generated output:
for_redistribution
— A
folder containing the installer to distribute the standalone application
for_testing
— A folder
containing the raw files generated by the compiler
for_redistribution_files_only
—
A folder containing only the files needed to redistribute the application
PackagingLog.txt
— A log
file generated by the compiler.
Open the for_redistribution
folder.
Run the installer.
In the folder containing the generated shared libraries,
create a new file called addmatrix.cpp
.
Using a text editor, open addmatrix.cpp
.
Copy the following into the file.
#include "addmatrix.h" int run_main(int argc, char **argv) { if (!mclInitializeApplication(NULL,0)) { std::cerr << "could not initialize the application properly" << std::endl; return -1; } if( !addmatrixInitialize() ) { std::cerr << "could not initialize the library properly" << std::endl; return -1; } try { // Create input data double data[] = {1,2,3,4,5,6,7,8,9}; mwArray in1(3, 3, mxDOUBLE_CLASS, mxREAL); mwArray in2(3, 3, mxDOUBLE_CLASS, mxREAL); in1.SetData(data, 9); in2.SetData(data, 9); // Create output array mwArray out; // Call the library function addmatrix(1, out, in1, in2); std::cout << "The value of added matrix is:" << std::endl; std::cout << out << std::endl; } catch (const mwException& e) { std::cerr << e.what() << std::endl; return -2; } catch (...) { std::cerr << "Unexpected error thrown" << std::endl; return -3; } addmatrixTerminate(); mclTerminateApplication(); return 0; } int main() { mclmcrInitialize(); return mclRunMain((mclMainFcnType)run_main,0,NULL); }
Use mbuild
to compile and link the
application.
mbuild addmatrix.cpp addmatrix.lib
From the system's command prompt, run the application.
addmatrix The value of added matrix is: 2 8 14 4 10 16 6 12 18