Create a C/C++ Application with MATLAB Code

This example shows how to create a C/C++ shared library using a MATLAB® function. You then integrate it into an application.

  1. In MATLAB, examine the MATLAB code that you want to deploy as a shared library.

    1. Open addmatrix.m.

      function a = addmatrix(a1, a2)
      
      a = a1 + a2;
    2. At the MATLAB command prompt, enter addmatrix(1,2).

      The output appears as follows:

      ans =
      
           3
  2. Open the Library Compiler app.

    1. On the toolstrip, select the Apps tab.

    2. Click the arrow at the far right of the tab.

    3. Click Library Compilers.

  3. In the Application Type section of the toolstrip, select C++ Shared Library from the list.

  4. Specify the MATLAB functions you want to deploy.

    1. In the Exported Functions section of the toolstrip, click the plus button.

    2. In the file explorer that opens, locate and select the addmatrix.m file.

      addmatrix.m is located in matlabroot\extern\examples\compilersdk.

    3. Click Open to select the file and close the file explorer.

  5. 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.

  6. Click Package.

    The Package window opens while the library is being generated.

  7. Select the Open output folder when process completes check box.

  8. Click Close on the Package window.

  9. 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.

  10. Open the for_redistribution folder.

  11. Run the installer.

  12. In the folder containing the generated shared libraries, create a new file called addmatrix.cpp.

  13. Using a text editor, open addmatrix.cpp.

  14. 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);
    }
  15. Use mbuild to compile and link the application.

    mbuild addmatrix.cpp addmatrix.lib
  16. 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
Was this topic helpful?