Is there an example on how to use C++ shared libraries with Microsoft Visual C++ 2005 Professional or Express editions?

3 views (last 30 days)
I am trying to use MATLAB Compiler generated C++ shared libraries with Microsoft Visual C++ 2005 Professional or Express editions. I would like to know the steps to setup the project.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Feb 2021
Edited: MathWorks Support Team on 18 Feb 2021
- MATLAB Compiler can generate two types of shared libraries: C shared libraries and C++ shared libraries.
web([docroot '/toolbox/compiler/f2-9676.html'])
- When using LCC compiler to compile your libraries, remember that it is a C-compiler, and it cannot compile C++:
- In order to use the MATLAB functions contained in a shared library, you need a “driver application” that initializes the MCR, etc. Search for the functions mclmcrInitialize and mclInitializeApplication in MATLAB documentation to get more information.
- When working with C++ shared libraries: Due to name mangling in C++, you must compile your driver application with the same version of your third-party compiler that you use to compile your C++ shared library in MATLAB.
web([docroot '/toolbox/compiler/f2-995712.html'])
- Both C and C++ shared libraries can be used with a driver application created in Microsoft Visual C++. The difference is that if you created a C shared library, you will need to use mxArray (pointer) as data type to access your data, whereas if you create a C++ shared library, you can access your data by using the mwArray data type.
- When compiling, the linker will need to find certain required libraries, for this you will need to add them under Project -> Properties -> Linker -> Input -> Additional dependencies:
For example: mclmcrrt.lib, mclmcr.lib, libmatrixp.lib (see included screen shot).
- If you get linking errors LNK2019, try to identify the function that cannot be resolved and see to which library it belongs, then add the library to the linker list above.
- When using Visual Studio Express Edition, the Platform SDK will need to be installed in order to be able to compile:
Microsoft Windows Server 2003 SP1 Platform SDK ISO Install
https://www.microsoft.com/en-in/download/details.aspx?id=15656
- Visual Studio Express will also require you to add additional entries to the project’s configuration, for example:
1) Project -> Properties -> C/C++ -> General -> Additional Include Directories:
C:\Microsoft Platform SDK\Include;C:\MATLAB\R2008a\extern\include;" C:\VC2005MatrixDriver8a"
2) Project -> Properties -> Linker -> General -> Additional Include Directories:
C:\Microsoft Platform SDK\Include;C:\MATLAB\R2008a\extern\include;" C:\VC2005MatrixDriver8a"
3) Project -> Properties -> Debugging:
PATH= “C:\VC2005MatrixDriver8a”;%PATH%
- When running your driver application, the application will need to be able to locate the shared library. For this, make sure that the path to the shared library has been specified under Project -> Properties -> Configuration Properties -> Debugging -> Environment (see included screen shot). Alternatively (or for deployment), you might want to copy your executable to the same folder that contains the shared library.
- The included R2008a example will run out-of-the-box if you move it to the “C:\VC2005MatrixDriver8a” folder. If you are running it from another folder, make sure you adjust all project settings and described and shown in the included screen shots. To compile the shared library, use the included COMPILECPPLIB function.

More Answers (0)

Categories

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

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!