How do I use a C++ shared library that was generated with MATLAB Compiler 4.0 (R14) with MSVC 6.0?

2 views (last 30 days)
I would like to convert MATLAB files into a C++ shared library that can be called from my Microsoft Visual C/C++ project. I would like to see a complete example that lists the steps necessary to do this.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jan 2010
This example shows how to generate a C++ shared library with MATLAB Compiler 4.0 (R14) and use it in an MSVC 6.0 project.
1. Execute:
mbuild -setup
from the MATLAB command prompt and select the MSVC 6.0 compiler.
2. Copy these files from $MATLABROOT/extern/examples/compiler to your build directory:
$MATLABROOT/extern/examples/compiler/addmatrix.m
$MATLABROOT/extern/examples/compiler/multiplymatrix.m
$MATLABROOT/extern/examples/compiler/eigmatrix.m
$MATLABROOT/extern/examples/compiler/matrixdriver.cpp
(where $MATLABROOT 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. Start the Microsoft Visual C/C++ 6.0 IDE.
5. From the File menu, select New. Click on the Projects tab. Select Win32 Console Application. In the Project Name field, type "matrixdriver". Also, verify that Create new workspace is filled in and that Win32 is checked in the Platforms field. Click OK.
6. Choose Empty Project as the console application to create, then click Finish, and then click OK.
7. Select the File View tab in the left-hand frame and double-click on matrixdriver files. Select Source Files in the tree underneath matrixdriver files, and then select Project from the menubar. In the dropdown menu, select Add to Project and then click on Files. Select the matrixdriver.cpp file that resides in your build directory from step 2.
8. Right-click on matrixdriver files and select Add Files to Project. Navigate to the directory in which you compiled the shared library and select:
libmatrix.lib
Also, add the following file located in $MATLABROOT\extern\lib\win32\microsoft\msvc60:
mclmcrrt.lib
In MATLAB 7.2 (R2006a) and later, this file is located in $MATLABROOT\extern\lib\win32\microsoft.
9. Highlight test files and right-click. Select Settings. Click on the C/C++ Tab. In the Category listbox select Code Generation. In the Use Runtime library listbox, select Multithreaded DLL.
Now change the Category listbox to Preprocessor. Add to the Additional include directories field:
$MATLABROOT\extern\include
Also add to the Additional include directories field the directory in which libmatrix.h is located. This should be your work directory from step 2. Click OK.
10. Select Build from the menubar and in the dropdown menu select Rebuild All.
11. You should now have built matrixdriver.exe.
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.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!