Interfacing a C++ library with Matlab

12 views (last 30 days)
Aurélien
Aurélien on 17 Oct 2022
Commented: Aurélien on 21 Oct 2022
Hello,
I have a simple C++ library, comprised of a header (.h) and a source file (.cpp). It uses C++ strings, and if possible I would like to keep it that way (meaning not have to modify it to use C strings instead). The library was compiled using Visual Studio MSVC 142, and I am using Matlab R2022a. I want to use this library in Matlab, and be able to export it to some other machines.
I followed this guide to generate a Matlab interface .dll directly from Matlab : https://fr.mathworks.com/help/matlab/matlab_external/steps-to-publish-a-matlab-interface-to-a-c-library.html . I then followed this guide to make the libraryInterface.dll into a toolbox, which would be the most optimal way to distribute it the way I would want to : https://fr.mathworks.com/help/matlab/matlab_prog/create-and-share-custom-matlab-toolboxes.html .
Installing the toolbox worked instantly on my machine. I was able to call my functions using clib.libraryName.functionName in a Matlab script with no issue whatsoever, everything worked as intended. Then I proceeded to export the libraryInterface.dll and the exact same script I ran on my machine onto another PC, to test if it would work. This PC has no C++ compiler installed, and runs Matlab R2019b. I got an error on the first clib call :
Unable to resolve the name clib.libraryName.functionName.
The .dll is installed correctly, and included in the path.
Here are my questions : Should what I did have worked, and if so would you have any idea what went wrong ? Does the new PC needs a MSVC compiler ? Or an up-to-date version of Matlab ?

Accepted Answer

Harsha
Harsha on 20 Oct 2022
Creating an interface from header and source CPP files and support for .dll compiled libraries were added to MATLAB from R2021a release.
When using an interface created in R2022a in an older release, the following error will be displayed.
“Invalid or incompatible interface library, possibly created in a newer version of MATLAB. Rebuild the interface library for the current version.”
As new options and parameters were added when generating the interface in R2022a, the generated interface will not work in the previous releases. However, the interfaces generated in older releases will work fine in the later releases.
Using up-to-date version of MATLAB will resolve this issue.
Check version History section in this link to know features added and supported in different versions of MATLAB. https://in.mathworks.com/help/matlab/ref/clibgen.generatelibrarydefinition.html
  1 Comment
Aurélien
Aurélien on 21 Oct 2022
Thanks ! We upgraded the new PC to Matlab 2022b and it worked like a charm.

Sign in to comment.

More Answers (0)

Categories

Find more on Manage Products in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!