MATLAB: How to use a C++ shared library that was generated with MATLAB Compiler 4.5 (R2006b) using Borland C++ Builder Version 6.0

borlandlibraryMATLAB Compilershared

I would like to convert MATLAB files into a C++ shared library that can be called from my BORLAND C++ Builder Version 6.0 project. I would like to see a complete example that lists the steps necessary to do this.

Best Answer

This example shows how to generate a C++ shared library with MATLAB Compiler 4.5 (R2006b) and use it in an Borland C++ Builder Version 6.0 project.
1. Execute:
mbuild –setup
from the MATLAB command prompt and select the BORLAND compiler.
2. Copy these files from $MATLAB/extern/examples/compiler to your build directory:
$MATLAB/extern/examples/compiler/addmatrix.m
$MATLAB/extern/examples/compiler/multiplymatrix.m
$MATLAB/extern/examples/compiler/eigmatrix.m
$MATLAB/extern/examples/compiler/matrixdriver.cpp
(where $MATLAB is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.)
3. To create the C++ shared library, use:
mcc -W cpplib:libmatrixp -T link:lib addmatrix.m multiplymatrix.m eigmatrix.m –v
4. Add the following directory locations to your PATH environment variable
$MATLAB\extern\include;$MATLAB\extern\lib\win32\borland;
The path variable is found by right clicking My Computer -> Properties, selecting the Advanced tab and clicking Environment Variable.
5. Start the BORLAND C++ Builder IDE.
6. From the File menu, select New. Select the option Other. Select the Console Wizard. In the Console Wizard options check the following options: C++, Multi Threaded and Console Application.
7. Remove the default Unit1.cpp file that is created from the project by selecting Project->Remove from Project -> Unit1.cpp.
8. Add the following files to your project by selecting Project -> Add to Project
matrixdriver.cpp
Navigate to the directory in which you compiled the shared library and select:
libmatrixp.lib
libmatrixp.h
Add the following files located in $MATLAB\extern\lib\win32\borland:
mclmcrrt.lib
9. Project Settings:
Before the project is built please ensure the following
Select the Project->Options
a) Select the Directories/Conditional Tab. Add the following directory locations
Include Path : $workfolder;$MATLAB\extern\include;
Library Path : $workfolder;$MATLAB\extern\lib\win32\borland;
where $workfolder is the location of the folder where the libmatrix.lib was built.
b)Select the Compiler tab and ensure that the treat ‘enum types as int’ checkbox has been checked.
10. Select Project ->Build.
11. You should now have built PROJECT_NAME.exe where PROJECT_NAME is the name of your project.
12. Before executing matrixdriver.exe, you may need to place libmatrix.dll and libmatrix.ctf (created in step 3) in the same location as matrixdriver.exe